Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny checkboxGroupInput - select all checkboxes by click

Tags:

r

shiny

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)
    }
    })


})
like image 564
Yoav Avatar asked Mar 03 '15 10:03

Yoav


2 Answers

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))
like image 136
Pork Chop Avatar answered Nov 06 '22 20:11

Pork Chop


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)
like image 36
NicE Avatar answered Nov 06 '22 18:11

NicE