R Shiny checkboxGroupInput - select 单击所有复选框

R Shiny checkboxGroupInput - select all checkboxes by click

我有一个包含 checkboxGroupInput 的 R Shiny 应用程序,我正在尝试使用 updateCheckboxGroupInput 函数创建一个 "select all" 按钮。 你可以在下面看到完整的代码,但基本上我是这样定义 cb 组的:

checkboxGroupInput("campaigns","Choose campaign(s):",campaigns_list)

然后,点击按钮,调用函数:

updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list,selected=campaigns_list)

我有一个迹象表明函数 运行,但它所做的实际上是取消选中复选框。 顺便说一句,当我把选择放在定义 cbGroupInput 上时,它确实起作用了,但不是函数。

谢谢!

这是我的 server.R:

library(shiny)
source('usefulFunctions.R')
shinyServer(function(input, output, session) {

  output$cascading <- renderUI({
    provider_id <- input$provider
    if (provider_id == "") return(NULL)
    campaigns_list <<- t(getCampaigns(provider_id))
    tagList(
      checkboxGroupInput("campaigns","Choose campaign(s):", 
                         choices = campaigns_list, selected = campaigns_list),
      actionLink("selectall","Select All")
      )
  })

  observe({
    if(is.null(input$selectall)) return(NULL)
    if (input$selectall > 0)
    {
      print(campaigns_list)
      updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list,selected=campaigns_list)
    }
    })


})

如果 campaigns_list 是一个列表,可能是因为您在 updateCheckboxGroupInput.

尝试用 selected=unlist(campaigns_list) 替换 selected=campaigns_list

这是一个带有虚拟名称的示例:

library(shiny)
server<-function(input, output,session) {
  observe({
    if(input$selectall == 0) return(NULL)
    else if (input$selectall > 0)
    {
      variables<-list("Cylinders" = "cyl","Transmission" = "am","Gears" = "gear")
      updateCheckboxGroupInput(session,"variable","Variable:",choices=variables,selected=unlist(variables))
    }
  })
}

ui <- shinyUI(fluidPage(        
    checkboxGroupInput("variable", "Variable:",list("Cylinders" = "cyl","Transmission" = "am","Gears" = "gear")),
    actionButton("selectall", "Select All")
))
shinyApp(ui = ui, server = server)

我还在此处添加了 select 和 unselect 选项,方法是检查按钮或 link 是否可以被 2

整除
#rm(list = ls())
library(shiny)
campaigns_list <- letters[1:10]

ui =fluidPage(
  checkboxGroupInput("campaigns","Choose campaign(s):",campaigns_list),
  actionLink("selectall","Select All") 
)
server = function(input, output, session) {

  observe({
    if(input$selectall == 0) return(NULL) 
    else if (input$selectall%%2 == 0)
    {
      updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list)
    }
    else
    {
      updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list,selected=campaigns_list)
    }
  })
}
runApp(list(ui = ui, server = server))