【问题标题】:Shiny R determine which checkbox was tickedShiny R 确定勾选了哪个复选框
【发布时间】:2018-08-23 23:13:35
【问题描述】:

我想知道点击了哪个复选框组。例如,当点击组“a”时; “b”然后我想将它存储在一个来自“subset_one”的变量中。

有什么想法吗?

library(shiny)
  obs_ev <- "c(input[['subset_one']], 
               input[['subset_two']], 
               input[['subset_three']])"
  shinyApp(
    shinyUI(
      fluidPage(
        uiOutput('ui')
      )
    ),
    shinyServer(function(session, input, output) {

      output$ui <- renderUI({
        inputPanel(
          checkboxGroupInput('subset_one',   'Subset:', choices = c("a", "b")),
          checkboxGroupInput('subset_two',   'Subset:', choices = c("c", "d")),
          checkboxGroupInput('subset_three', 'Subset:', choices = c("e", "f"))
        )
      })

      observeEvent(eval(parse(text = obs_ev)), {
        print("1")
        print(input[['subset_one']])
        print("2")
        print(input[['subset_two']])
        print("3")
        print(input[['subset_three']])

        dat  <- eval(parse(text = obs_ev))

        print(dat)

      }, ignoreNULL = FALSE)
    })
  )

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    如果您想知道触发了什么,我认为您最好为每个要观察的对象设置不同的观察者,然后设置一个可以处理实际观察者之外的所有响应的函数。例如,考虑这个解决方案

    library(shiny)
    obs_list <- c("subset_one","subset_two","subset_three")
    shinyApp(
      shinyUI(
        fluidPage(
          uiOutput('ui')
        )
      ),
      shinyServer(function(session, input, output) {
    
        output$ui <- renderUI({
          inputPanel(
            checkboxGroupInput('subset_one',   'Subset:', choices = c("a", "b")),
            checkboxGroupInput('subset_two',   'Subset:', choices = c("c", "d")),
            checkboxGroupInput('subset_three', 'Subset:', choices = c("e", "f"))
          )
        })
    
        lapply(obs_list, function(obs) {
          observeEvent(input[[obs]], {changeVal(obs, input[[obs]])}, ignoreNULL = FALSE)
        })
    
        changeVal <- function(obj, val) {
          print(paste("changed", obj, "val", paste(val, collapse=",")))
          dat  <- do.call("c", lapply(obs_list, function(x) input[[x]]))
    
          print(dat)
        }
    
      })
    )
    

    请注意,我摆脱了 eval(parse()) 的东西。我尽量避免这种情况。但这里我们使用lappy 为每个输入构建不同的观察者。然后我们调用一个函数来处理响应,指示实际点击了哪个对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      • 2010-12-15
      • 2014-03-12
      • 2021-02-14
      • 1970-01-01
      • 2013-06-21
      相关资源
      最近更新 更多