R 在列表中同时选中所有复选框

R 在列表中同时选中所有复选框,r,widget,shiny,R,Widget,Shiny,我想知道如何一次选中所有复选框。在我的代码中,我有五个复选框 server <- function(input, output) { output$distPlot <- renderPlot({ hist(rnorm(input$obs), col = 'darkgray', border = 'white') }) } ui <- fluidPage( sidebarLayout( sidebarPanel( sliderInput

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

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)

server我会在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事件中对此进行bing,我会在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事件中使用它,这远没有Jorel的回答那么优雅,但它是一个使用纯
闪亮的
包代码的解决方案

    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)
库(闪亮)
#*确保将session作为参数包含在内,以便使用update函数

服务器这远不如Jorel的回答那么优雅,但它是一个使用纯
闪亮的
包代码的解决方案

    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)
库(闪亮)
#*确保将session作为参数包含在内,以便使用update函数

谢谢你的评论。如果有效的话,这将被视为一个答案。如果你能让它工作,我会很高兴。请告诉我它是怎么回事:)我不知道JS,所以没有尝试。谢谢你的评论。如果有效的话,这将被视为一个答案。如果你能让它工作起来,我会很高兴。请告诉我它是如何工作的:)我不知道JS,所以没有尝试。
错误:在“shinyApp”中出现意外符号。
对不起。我丢失了
侧边栏面板的关闭
。它现在应该可以工作了。谢谢,但是部分工作
1)
取消选中
All
而不是取消选择其他
2)
检查前四个,而不是选择第五个i e
All
@AaghazHussain也许在这种行为的复选框上设置操作按钮会更好。查看我的后续信息。
错误:在“shinyApp”中出现意外符号。
抱歉。我丢失了
侧边栏面板的关闭
。它现在应该可以工作了。谢谢,但是部分工作
1)
取消选中
All
而不是取消选择其他
2)
检查前四个,而不是选择第五个i e
All
@AaghazHussain也许在这种行为的复选框上设置操作按钮会更好。请看我的后续报道。