R 确定勾选了哪个复选框

R 确定勾选了哪个复选框,r,shiny,R,Shiny,我想知道单击了哪个复选框组。例如,单击a组时;然后我想存储在一个变量中,它来自于子集1 有什么想法吗 library(shiny) obs_ev <- "c(input[['subset_one']], input[['subset_two']], input[['subset_three']])" shinyApp( shinyUI( fluidPage( uiOutput('ui

我想知道单击了哪个复选框组。例如,单击a组时;然后我想存储在一个变量中,它来自于子集1

有什么想法吗

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)
    })
  )

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

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)
    }

  })
)

注意,我去掉了evalpasse的东西。如果可能的话,我会尽量避免。但在这里,我们使用lappy为每个输入构建不同的观察者。然后我们调用一个函数来处理响应,指示实际单击了哪个对象。

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

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)
    }

  })
)
注意,我去掉了evalpasse的东西。如果可能的话,我会尽量避免。但在这里,我们使用lappy为每个输入构建不同的观察者。然后我们调用一个函数来处理响应,指示实际单击了哪个对象