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

时间:2016-01-27 17:05:43

标签: r widget shiny

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

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),屏幕应该是这样的 enter image description here

2 个答案:

答案 0 :(得分:4)

这并不像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. 在复选框中,如果您选中&#34;全部&#34;,则表示您希望检查所有框,但我不认为取消选择&#34;全部&#34;必然意味着取消选择所有方框。
  2. 从1)开始,你试图让一个控件做两件不同的事情,这可能会打开混乱的大门。
  3. 所以这里是我的建议:用户四个复选框和两个按钮。如果您选择全部或取消选中所有框,这两个按钮将控制它们,它们会独立运作。

    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)
    

答案 1 :(得分:0)

我会在JS方面这样做。我会得到这样的每个复选框 var cbx1 = document.getElementById('checkbox1');等我将它们存储在一个数组中 我还有一个能检查所有内容的功能:

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

我会在第四个复选框onclick事件上绑定此功能。我还有一个功能,检查是否每个都被检查:

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

我会在每个复选框的onclick事件上发布这个