一个闪亮的应用程序中的多个reactiveValues

The*_*Man 5 r shiny

reactiveValues经常在 Shiny 中使用,因为它们比inputoutput对象更灵活。嵌套的reactiveValues 很棘手,因为任何孩子的任何变化也会触发与父母相关的反应。为了解决这个问题,我尝试制作两个不同的reactiveValues对象(不是同一个列表中的两个对象,而是两个不同的列表),它似乎正在工作。我找不到任何这样的例子,想知道它是否应该以这种方式工作。是否有任何可能因此出现的问题?

在这个应用程序中,有两个反应值对象 -reac1reac2。他们每个人都与一个下拉,column1column2分别。更改column1column2与最新更新时间的反应值,更新的情节,并在打印的最新值reac1reac2

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)
Run Code Online (Sandbox Code Playgroud)

Edu*_*gel 3

ReactiveValues 就像 input$ 的读/写版本,您可以在一个 ReactValue 列表中拥有多个“独立”变量。因此,您的示例中不需要两个无功值。请参阅下面的代码。

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)
Run Code Online (Sandbox Code Playgroud)