R Shiny checkboxGroupInput - select all checkboxes by clicking

I have an R Shiny application that contains checkboxGroupInput, and I'm trying to create a "select all" button using the updateCheckboxGroupInput function. You can see the full code below, but basically I defined the cb groups as follows:

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

and then, by pressing the button, call the function:

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

I have an indication that the function works, but what it does is actually not displaying the flags. BTW, when I put the highlight in the definition of cbGroupInput, it really worked, but not from the function.

Thanks!

this is my .R server:

 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) } }) }) 
+5
source share
2 answers

I also added select and unselect options here, setting the button or link to be divisible by 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)) 
+7
source

If campaigns_list is a list, perhaps because you are listing all of your options instead of the value of the fields that should be selected in the selected argument of your updateCheckboxGroupInput .

Try replacing selected=campaigns_list with selected=unlist(campaigns_list) .

Here is an example with fictitious names:

 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) 
+2
source

Source: https://habr.com/ru/post/1214551/


All Articles