我有一个响应式(Reactive)表达式,我想从其他两个响应式(Reactive)表达式中的任何一个取值,最近已更改。我做了下面的例子:

ui.r:

shinyUI(bootstrapPage(
column(4, wellPanel(
  actionButton("button", "Button"),
  checkboxGroupInput("check", "Check", choices = c("a", "b", "c"))
)),
column(8,
  textOutput("test")
)
))

和server.r:
shinyServer(function(input, output) {
 output$test <- renderText({
  # Solution goes here
 })
})

我希望输出显示button(单击按钮的次数)或check(显示选中的框的字符向量)的值,具体取决于最近更改的内容。

最佳答案

您可以使用reactiveValues跟踪按钮的当前状态来实现此目的:

library(shiny)
runApp(list(ui = shinyUI(bootstrapPage(
  column(4, wellPanel(
    actionButton("button", "Button"),
    checkboxGroupInput("check", "Check", choices = c("a", "b", "c"))
  )),
  column(8,
         textOutput("test")
  )
))
, server = function(input, output, session){
  myReactives <- reactiveValues(reactInd = 0)
  observe({
    input$button
    myReactives$reactInd <- 1
  })
  observe({
    input$check
    myReactives$reactInd <- 2
  })
  output$test <- renderText({
    if(myReactives$reactInd == 1){
      return(input$button)
    }
    if(myReactives$reactInd == 2){
      return(input$check)
    }
  })
}
)
)

关于r - 在Shiny中选择最近更改的响应式(Reactive),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25672304/

10-11 17:16