在 Shiny 中一次选中所有复选框

Selecting all the check-boxes at once in Shiny

我想知道如何一次 select 所有复选框。在我的代码中,我有五个复选框。

server <- function(input, output) {
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      checkboxInput("checkbox1", label = "meanSNR", value= FALSE),
      checkboxInput("checkbox2", label = "t-statistics", value = FALSE),
      checkboxInput("checkbox3", label = "adjusted p-value", value = FALSE),
      checkboxInput("checkbox4", label = "log-odds", value = FALSE),
      checkboxInput("checkbox5", label = "All", value = FALSE)),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)

我想知道如何让它发挥作用

1) 如果用户select选择第五个复选框All,它应该自动select所有的复选框。取消选中时,它应该取消select所有复选框。

2 ) 如果用户 select 前四个复选框,它也应该 select 第五个 All 复选框。

对于条件 1) ,屏幕应该是这样的

我会在 JS 端执行此操作。我会像这样得到每个复选框 var cbx1 = document.getElementById('checkbox1'); 等,我会将它们存储在一个数组中 我还会有一个功能来检查所有内容:

checkEverything = function(){
    cbx1.val = "true";
    cbx2.val = "true"; 
    // etc..
}

我会在第 4 个复选框 onclick 事件上绑定此函数。我还有一个函数来检查是否每个都被选中,如:

checkIfEverythingChecked = function(){
   if(cbx1.val == true && cbx2.val == true)
      cbx4.val = true; 
}

而且我会 bing 在每个复选框的 onclick 事件上执行此操作

这不像 Jorel 的回答那么优雅,但它是一个使用纯 shiny 包代码的解决方案。

    library(shiny)
#* make sure to include session as an argument in order to use the update functions
server <- function(input, output, session) { 
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })

  #* This observer will update checkboxes 1 - 4 to TRUE whenever checkbox 5 is TRUE
  observeEvent(
    eventExpr = input$checkbox5,
    handlerExpr = 
    {
      if (input$checkbox5)
      lapply(paste0("checkbox", 1:4),
             function(x)
             {
               updateCheckboxInput(session, x, value = input$checkbox5)
             }
      )
    }
  )

  #* This observer will set checkbox 5 to FALSE whenever any of checkbox 1-4 is FALSE
  lapply(paste0("checkbox", 1:4),
         function(x) 
          {
            observeEvent(
              eventExpr = input[[x]], 
              handlerExpr = 
              {
                if (!input[[x]]) updateCheckboxInput(session, "checkbox5", value = FALSE)
              }
            )
          }
  ) 
}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      checkboxInput("checkbox1", label = "meanSNR", value= FALSE),
      checkboxInput("checkbox2", label = "t-statistics", value = FALSE),
      checkboxInput("checkbox3", label = "adjusted p-value", value = FALSE),
      checkboxInput("checkbox4", label = "log-odds", value = FALSE),
      checkboxInput("checkbox5", label = "All", value = FALSE)
    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)

一些跟进和建议

我花了一点时间试图让应用程序执行您指定的操作,但老实说,它感觉很不自然(并且工作得不是特别好)。

  1. 在复选框中,如果您选中"All",则表示您希望选中所有复选框,但我认为unselecting "All"不一定意味着un select检查所有框。
  2. 从 1) 开始,您试图让一个控件做两件不同的事情,这可能会导致混淆。

所以这是我的建议:使用四个复选框和两个按钮。如果您 select 全部或取消 select 所有框,这两个按钮将控制它们,并且它们独立运行。

library(shiny)
#* make sure to include session as an argument in order to use the update functions
server <- function(input, output, session) { 
  output$distPlot <- renderPlot({
    hist(rnorm(input$obs), col = 'darkgray', border = 'white')
  })

  #* This observer will update checkboxes 1 - 4 to TRUE whenever selectAll is clicked
  observeEvent(
    eventExpr = input$selectAll,
    handlerExpr = 
    {
      lapply(paste0("checkbox", 1:4),
             function(x)
             {
                 updateCheckboxInput(session = session, 
                                     inputId = x, 
                                     value = TRUE)
             }
      )
    }
  )

  #* This observer will update checkboxes 1 - 4 to FALSE whenever deselectAll is clicked
  observeEvent(
    eventExpr = input$deselectAll,
    handlerExpr = 
    {
      lapply(paste0("checkbox", 1:4),
             function(x)
             {
                 updateCheckboxInput(session = session, 
                                     inputId = x, 
                                     value = FALSE)
             }
      )
    }
  )


}

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
      checkboxInput("checkbox1", label = "meanSNR", value= FALSE),
      checkboxInput("checkbox2", label = "t-statistics", value = FALSE),
      checkboxInput("checkbox3", label = "adjusted p-value", value = FALSE),
      checkboxInput("checkbox4", label = "log-odds", value = FALSE),
      actionButton("selectAll", label = "Select All"),
      actionButton("deselectAll", label = "Deselect All")
    ),
    mainPanel(plotOutput("distPlot"))
  )
)

shinyApp(ui = ui, server = server)