Yoav
Yoav

Reputation: 1029

R Shiny checkboxGroupInput - select all checkboxes by click

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

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

and then, on a button click, call the function:

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

I have an indication that the function ran, but what it does is actually UNselecting the checkboxes. BTW, when I put the selected upon defining the cbGroupInput it did worked, but not on the function.

Thanks!

this is my 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)
    }
    })


})

Upvotes: 14

Views: 28240

Answers (2)

Pork Chop
Pork Chop

Reputation: 29387

I also added the select and unselect options here by checking if the button or link are 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))

Upvotes: 23

NicE
NicE

Reputation: 21425

If campaigns_list is a list, might be because you are specifying the list of all your choices instead of the value of the boxes that should be selected in the selected argument of your updateCheckboxGroupInput.

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

Here is an example with dummy 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)

Upvotes: 2

Related Questions