Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple reactiveValues In a Shiny App

Tags:

r

shiny

I use reactiveValues in Shiny a lot as they are more flexible than just the input and output objects. Nested reactiveValues are tricky since any changes in any of the children also triggers the reactivity linked to the parents. To get around this, I tried to make two different reactiveValues objects ( not two objects in the same list, but two different lists altogether ) and it seems to be working. I'm not able to find any example of this and want to find out if it's suppose to work this way. Are there any issues that might arise because of this?

In this app, there are two reactive values objects - reac1 and reac2. Each of them are linked to a drop down, column1 and column2 respectively. Changing column1 or column2 updates the reactive values with the latest time, updates the plot, and prints the latest values in reac1 and reac2.

ui = fluidPage(
  titlePanel("Multiple reactive values"),
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "column1", "Reac1", letters, selected = "a"),
      selectInput(inputId = "column2", "Reac2", letters, selected = "a")
    ),
    mainPanel(
      plotOutput("plot1")
    )
  )
)

server = function(input, output, session) {
  reac1 <- reactiveValues(asdasd = 0)
  reac2 <- reactiveValues(qweqwe = 0)

  # If any inputs are changed, set the redraw parameter to FALSE
  observe({
    input$column2
    reac2$qweqwe = Sys.time()
  })

observe({
    input$column1
    reac1$asdasd = Sys.time()
  })


  # Only triggered when the copies of the inputs in reac are updated
  # by the code above
  output$plot1 <- renderPlot({
      print(paste(reac1$asdasd, 'reac1'))
      print(paste(reac2$qweqwe, 'reac2'))
      hist(runif(1000))
  })
}
shinyApp(ui, server)
like image 259
TheComeOnMan Avatar asked Oct 17 '22 23:10

TheComeOnMan


1 Answers

ReactiveValues are like a read/write version of input$, and you can have several 'independent' variables inside one reactiveValue list. So, you do not need two reactive values in your example. See code below.

ui = fluidPage(
  titlePanel("Multiple reactive values"),
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "column1", "Reac1", letters, selected = "a"),
      selectInput(inputId = "column2", "Reac2", letters, selected = "a")
    ),
    mainPanel(
      verbatimTextOutput("txt1"),
      verbatimTextOutput("txt2")
    )
  )
)

server = function(input, output, session) {
  reac <- reactiveValues()
  #reac2 <- reactiveValues(qweqwe = 0)

  # If any inputs are changed, set the redraw parameter to FALSE

  observe({ 
    reac$asdasd =  input$column1
  })  
  observe({ 
    reac$qweqwe = input$column2
  }) 

  # Only triggered when the copies of the inputs in reac are updated
  # by the code above
  output$txt1 <- renderPrint({
    print('output 1')
    print(paste(reac$asdasd, 'reac1'))  
  })

  output$txt2 <- renderPrint({ 
    print('output2') 
    print(paste(reac$qweqwe, 'reac2')) 
  }) 

}
shinyApp(ui, server)
like image 167
Eduardo Bergel Avatar answered Oct 27 '22 19:10

Eduardo Bergel