Grec001
Grec001

Reputation: 1121

fail to pass reactive value to variables in shiny

I am trying to dynamically chop the image in shiny with imager::imsub. The result is empty image, according to the values printed, I realize that the values inputed fails to pass to the variable subArea as expected.

More worse, even the values defined in reactiveVlaues didn't show up. Doesn't the reactiveValues be regarded as initial values which will change when the environment and dependants changed?

Please find the toy sample for your reference.

Besides the empty image msg, I also get error msg (subscript) logical subscript too long which only show up when I make the imsub part uncommented, have no clue where the error comes from. Many thanks.

Rlogo

library(shiny)
library(shinydashboard)
library(imager)
{
rm(list=ls())

ui <- 
  dashboardPage(
    dashboardHeader(title = NULL),
    dashboardSidebar(
      sliderInput("ixs", "init x start", value = 50, min = 1, max = 250),
      sliderInput("ixr", "init x range", value = 50, min = 1, max = 250),
      sliderInput("iys", "init y start", value = 50, min = 1, max = 250),
      sliderInput("iyr", "init y range", value = 50, min = 1, max = 250),
      actionButton("submit", "submit")
    ),
    dashboardBody(
      plotOutput("pl"),
      textOutput("txt")
    )
  )

server <- 
  function(input, output, session) {

    #subArea <- list()
    im <- load.image("Rlogo.png")

    subArea <- reactiveValues(xs = 0, xr = 500, ys = 0, yr =500)

    observeEvent(input$submit,{
      subArea$xs <- input$ixs
      subArea$ys <- input$iys
      subArea$xr <- input$ixr
      subArea$yr <- input$iyr
    })
      subArea$xs <- 0
      subArea$ys <- 0
      subArea$xr <- dim(im)[1]
      subArea$yr <- dim(im)[2]

    output$txt <- 
      renderPrint(cat(c(
        paste0("   submit", input$submit  ),
        paste0("   dim =", dim(im)[1:2]  ),
        paste0("   ixs =", input$ixs),
        paste0("   ixr =", input$ixr),
        paste0("   iys =", input$iys),
        paste0("   iyr =", input$iyr),
        paste0("subArea$ixs =", subArea$ixs),
        paste0("subArea$ixr =", subArea$ixr),
        paste0("subArea$iys =", subArea$iys),
        paste0("subArea$iyr =", subArea$iyr)
        ))) 

    output$pl <- renderPlot({

      load.image("Rlogo.png") %>% 
        imsub( x > subArea$xs,
               x < subArea$rx,
               y > subArea$ys,
               y < subArea$yr) %>%
        plot
    })

  }

shinyApp(ui, server)
}

Upvotes: 0

Views: 182

Answers (1)

Kota Mori
Kota Mori

Reputation: 6750

The error is due to typos. See the below.

library(shiny)
library(shinydashboard)
library(imager)
{
  rm(list=ls())

  ui <- 
    dashboardPage(
      dashboardHeader(title = NULL),
      dashboardSidebar(
        sliderInput("ixs", "init x start", value = 50, min = 1, max = 250),
        sliderInput("ixr", "init x range", value = 50, min = 1, max = 250),
        sliderInput("iys", "init y start", value = 50, min = 1, max = 250),
        sliderInput("iyr", "init y range", value = 50, min = 1, max = 250),
        actionButton("submit", "submit")
      ),
      dashboardBody(
        plotOutput("pl"),
        textOutput("txt")
      )
    )

  server <- 
    function(input, output, session) {

      #subArea <- list()
      im <- load.image("Rlogo.png")

      subArea <- reactiveValues(xs = 0, xr = 500, ys = 0, yr =500)

      observeEvent(input$submit,{
        subArea$xs <- input$ixs
        subArea$ys <- input$iys
        subArea$xr <- input$ixr
        subArea$yr <- input$iyr
      })
      subArea$xs <- 0
      subArea$ys <- 0
      subArea$xr <- dim(im)[1]
      subArea$yr <- dim(im)[2]

      output$txt <- 
        renderPrint(cat(c(
          paste0("   submit", input$submit  ),
          paste0("   dim =", dim(im)[1:2]  ),
          paste0("   ixs =", input$ixs),
          paste0("   ixr =", input$ixr),
          paste0("   iys =", input$iys),
          paste0("   iyr =", input$iyr),
          #paste0("subArea$ixs =", subArea$ixs),
          #paste0("subArea$ixr =", subArea$ixr),
          #paste0("subArea$iys =", subArea$iys),
          #paste0("subArea$iyr =", subArea$iyr)
          paste0("subArea$ixs =", subArea$xs),
          paste0("subArea$ixr =", subArea$xr),
          paste0("subArea$iys =", subArea$ys),
          paste0("subArea$iyr =", subArea$yr)
      ))) 

      output$pl <- renderPlot({

        load.image("Rlogo.png") %>% 
          imsub( x > subArea$xs,
                 #x < subArea$rx,
                 x < subArea$xr,
                 y > subArea$ys,
                 y < subArea$yr) %>%
          plot
      })

    }

  shinyApp(ui, server)
}

Upvotes: 1

Related Questions