1

if I have a checkboxGroup defined as

 checkboxGroupInput("checkGroup", label = h2("Data Sets"), 
                     choices = list("Accounts Created" = 1, "Comments" = 2,
                                    "Submissions" = 3, "Votes" = 4),
                     selected = NULL)

Then on the server side, how do I access the selection status of the four boxes? Is it input$checkGroup[1], input$checkGroup[2], etc with 0/1 values? Or is input$checkGroup variable length containing only the values selected? Very confused as to how to determine if, for example, the first and third boxes are checked...

Mike H.
  • 13,960
  • 2
  • 29
  • 39
jerH
  • 1,085
  • 1
  • 12
  • 30

2 Answers2

1

They are treated as a character vector whose length is equal to the number of checked boxes. Here's a very simple shiny app showing you how checkboxGroups are treated:

library(shiny)
ui <- fluidPage(
  checkboxGroupInput("checkGroup", label = h2("Data Sets"), 
                     choices = list("Accounts Created" = 1, "Comments" = 2,
                                    "Submissions" = 3, "Votes" = 4),
                     selected = NULL),
  verbatimTextOutput("checkBoxText")
)

server <-  function(input, output, session) {
 output$checkBoxText <- renderText({input$checkGroup})
}

shinyApp( ui = ui, server = server, options = list(launch.browser = T) )

Notice how in the app the values are what are stored (not the labels). So if you select "Accounts Created", input$checkGroup will contain 1 and not "Accounts Created"

Mike H.
  • 13,960
  • 2
  • 29
  • 39
1

The output of checkboxGroupInput is a vector.

It is intended to be used when you may want to select more than one element at the same time.

To avoid confusion (and avoid letting our reactive have a run every time you select a box) it works best in conjunction with an action button:

  1. once you have done your selection (one or many more items);
  2. you can push the action button for execution. This makes sure that you process the user's selection only when the user is ready for it.

Also, sometime I prefer not to use a list, but just a simple vector, e.g.:

checkboxGroupInput("checkGroup", label = h2("Data Sets"), 
                     choices = c("Accounts Created", "Comments",
                                    "Submissions", "Votes"),
                     selected = NULL)

Then you can process on the server side the character vector, that will have the different value, e.g. input$checkGroup may be equal to c("Accounts Created", "Comments") if the first two boxes have been selected, and so on.

Perhaps the code I posted in reply to the following SO question may be of help R Shiny observeEvent issues

Please take into consideration that if many elements will be selected, you need to have a proper treatment pipeline. For example, if (or switch) only works for one elements at the time, and if you want to scan the output vector one element at the time, it works greatly. If you r treatment is more complex you need to think it through carefully.

Please do not hesitate to ask for more if it is not clear.

Community
  • 1
  • 1
Enzo
  • 2,543
  • 1
  • 25
  • 38
  • That makes sense, though I'm still running into issues implementing. I think my question now is beyond the OP, so I've posted a new one [here](http://stackoverflow.com/questions/43461690/ggplot-and-shiny-plots-not-appearing) – jerH Apr 18 '17 at 00:30