【问题标题】:Selecting all the check-boxes at once in Shiny在 Shiny 中一次选中所有复选框
【发布时间】:2016-01-28 02:52:40
【问题描述】:

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

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) 如果用户选中第五个复选框All,它应该自动选中所有复选框。在取消选中时,它应该取消选中所有复选框。

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

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

【问题讨论】:

    标签: r widget shiny


    【解决方案1】:

    这并不像 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. 在复选框中,如果选中“全部”,则表示您希望选中所有框,但我认为取消选择“全部”并不一定意味着取消选择所有框。
    2. 源于 1),您试图让一个控件执行两件不同的事情,这可能会导致混乱。

    所以这是我的建议:用户四个复选框和两个按钮。这两个按钮控制您是选择所有框还是取消选择所有框,并且它们独立操作。

    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)
    

    【讨论】:

    • Error: unexpected symbol in:" shinyApp"
    • 对不起。我失去了sidebarPanel 的关闭)。它现在应该可以工作了。
    • 谢谢,但部分工作 1) 取消选中 All 不取消选择其他人 2) 检查前四个,而不是选择第五个即。 All.
    • @AaghazHussain 也许你会更好地为这种行为在复选框上使用操作按钮。看我的跟进。
    【解决方案2】:

    我会在 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; 
    }
    

    我会在每个复选框的 onclick 事件中添加这个

    【讨论】:

    • 感谢您的 cmets。如果它有效,它将被视为一个答案。如果你让它工作,我会很高兴。
    • 请告诉我进展如何:)
    • 我不懂JS,所以没有尝试。
    猜你喜欢
    • 2020-11-20
    • 2019-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2020-05-20
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多