【问题标题】:Selecting most recently changed reactive expressions in Shiny在 Shiny 中选择最近更改的反应式表达式
【发布时间】:2014-09-05 05:10:44
【问题描述】:

我有一个反应式表达式,我想从最近更改的其他两个反应式表达式中的任何一个中获取它的值。我做了以下例子:

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(显示哪些框被选中的字符向量) 取决于最近更改的内容。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用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)
        }
      })
    }
    )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      相关资源
      最近更新 更多