4
votes

Why does this updateCheckboxGroupInput() code not work when more than 1 checkboxes are selected. When one checkbox from the above group is selected, the box below should update to show more specific entries. But if more than one checkbox is selected at the top, the code fails. Anyone knows what the issues might be?

Copy from here or bitbucket: https://bitbucket.org/snippets/afolabicrystal/deyay5

  ui <- fluidPage(
p("The first checkbox group controls the second"),
checkboxGroupInput("inCheckboxGroup", "Device Class",
                   c("TV", "Mobile", "Tablet")),
checkboxGroupInput("inCheckboxGroup2", "Device Types",
                   c("Amazon TV", "Apple TV", "Android Phone", "iPhone", "iPad", "Android Tablet")))


  server <- function(input, output, session) {
observe({
  x <- input$inCheckboxGroup


  # Can also set the label and select items
  updateCheckboxGroupInput(session, "inCheckboxGroup2",
                           label = paste("Device Types", length(x)),
                           if ('TV' %in% x) {
                             choices = c("Amazon TV", "Apple TV")
                           } else if("Mobile" %in% x) {
                             choices = c("Android Phone", "iPhone")
                           } else if("Tablet" %in% x) {
                             choices = c("iPad", "Android Tablet")
                           } else if (all(c("TV","Mobile") %in% x)) {
                             choices = c("Amazon TV", "Apple TV", "Android Phone", "iPhone")
                           } else if (all(c("TV","Tablet") %in% x)) {
                             choices = c("Amazon TV", "Apple TV", "iPad", "Android Tablet")
                           } else if (all(c("Mobile","Tablet") %in% x)) {
                             choices = c("Android Phone", "iPhone", "iPad", "Android Tablet")
                           } else if (all(c("TV", "Mobile", "Tablet") %in% x)) {
                             choices = c("Amazon TV", "Apple TV", "Android Phone", "iPhone", "iPad", "Android Tablet")
                           } else {
                             choices = character(0)
                           },
                           selected = choices
  )
})
}
shinyApp(ui, server)
1

1 Answers

5
votes

The problem is that you are using else if all the time; as soon as one of the ifs is evaluated the rest is skipped. Basically you are saying: If x is true, do y. But if x is not true (else), Check if z is true, etc... So if x actually is true, z is never evaluated. I hope that explanation makes sense.

Also, you can simplify your code by just checking for the three conditions (tv, mobile and tablet), and add the choices to a vector if the checkbox is selected.

A working example is given below. Hope this helps!


enter image description here


ui <- fluidPage(
  p("The first checkbox group controls the second"),
  checkboxGroupInput("inCheckboxGroup", "Device Class",
                     c("TV", "Mobile", "Tablet")),
  checkboxGroupInput("inCheckboxGroup2", "Device Types",
                     c())
)


server <- function(input, output, session) {
  observe({
    x <- input$inCheckboxGroup

    choices=c()
    if ('TV' %in% x) 
      choices = c(choices,c("Amazon TV", "Apple TV"))
    if("Mobile" %in% x) 
      choices =c(choices, c("Android Phone", "iPhone"))
    if("Tablet" %in% x) 
      choices = c(choices,c("iPad", "Android Tablet"))

    updateCheckboxGroupInput(session, "inCheckboxGroup2",
                             label = paste("Device Types", length(x)),
                             choices=choices,
                             selected = choices
    )
  })
}
shinyApp(ui, server)