Mickey_NC Mickey_NC - 1 month ago 10
R Question

R shiny updateCheckboxGroupInput does not work as expected

I wonder why all of my check boxes are not checked with this code ?

I have 1 checkboxGroupInput named "county" with 3 check boxes and another one
named "dynamic" with 33 check boxes. And I wonder why, when I check two or three check boxes of the "county" checkboxGroupInput, the dependent check boxes in the "dynamic" checkboxGroupInput are not checked. It works well only if one box is checked.

Thank you very much for helping.

This is the Server.R :

observeEvent(input$dynamic_county, {
if(is.element((input$dynamic_county),("South")))
{updateCheckboxGroupInput(session, inputId = "dynamic", selected =
list("02","03","05","06","09","13","16","17","18","21","28","29",
"32"))}})

observeEvent(input$dynamic_county, {
if(is.element((input$dynamic_county),("North")))
{updateCheckboxGroupInput(session, inputId = "dynamic", selected =
list("01","04","07","08","10","11","12","19","22","23","24","25","26",
"27","30","31","33"))}})

observeEvent(input$dynamic_county, {
if(is.element((input$dynamic_county),("East")))
{updateCheckboxGroupInput(session, inputId = "dynamic", selected =
list("14","15","20"))}})


Thank you again.

@ Warmoverflow, thank you for suggesting but it doesn't work, I tried this :

observeEvent(input$dynamic_county, {
if(is.element((input$dynamic_county),("South")))
{updateCheckboxGroupInput(session, inputId = "dynamic", selected =
list("02","03","05","06","09","13","16",
"17","18","21","27","28","29","32"))}

if(is.element((input$dynamic_county),("North")))
{updateCheckboxGroupInput(session, inputId = "dynamic", selected =
list("01","04","07","08","10","11","12","19",
"22","23","24","25","26","27","30","31","33"))}

if(is.element((input$dynamic_county),("East")))
{updateCheckboxGroupInput(session, inputId = "dynamic", selected =
list("14","15","20"))}
})


And I tried that too :

observeEvent(input$dynamic_county, {
ifelse((is.element((input$dynamic_county),
("South"))),updateCheckboxGroupInput(session, inputId = "dynamic",
selected = list("02","03","05","06","09","13","16",
"17","18","21","27","28","29","32")),


ifelse((is.element((input$dynamic_county),
("North"))),updateCheckboxGroupInput(session, inputId = "dynamic",
selected = list("01","04","07","08","10","11","12","19",
"22","23","24","25","26","27","30","31","33")),

ifelse((is.element((input$dynamic_county),
("East"))),updateCheckboxGroupInput(session, inputId = "dynamic",
selected = list("14","15","20")),
updateCheckboxGroupInput(session, inputId = "dynamic",selected =
NULL))))})


If I check 2 or the 3 check boxes, only one of them is executed.

Would you have an idea about the origin of the problem please ?

Thank you very much again.

Answer

Here is a working example. For example, when you select both "South" and "North", you need to select corresponding boxes for both. Code in your question only selects the boxes corresponding to one of them, thus the issue.

You also need to set ignoreNULL to FALSE for observeEvent, otherwise it won't get the event when you clear all boxes.

library(shiny)
library(stringr)

dynamic_choices = c(1:33)
dynamic_choices = str_pad(as.character(dynamic_choices), 2, side="left", pad="0")
selection = list("South"=c("02","03","05","06","09","13","16","17","18","21","28","29","32"),
                 "North"=c("01","04","07","08","10","11","12","19","22","23","24","25","26","27","30","31","33"),
                 "East"=c("14","15","20"))

ui <- shinyUI(fluidPage(

   titlePanel("Update CheckboxGroupInput"),

   sidebarLayout(
      sidebarPanel(
        checkboxGroupInput("county", "County", c("South", "North", "East")),
        checkboxGroupInput("dynamic", "Dynamic", dynamic_choices)
      ),

      mainPanel(
      )
   )
))

server <- shinyServer(function(session, input, output) {

  observeEvent(input$county, {
    selected = c()

    for (s in input$county) {
      selected = c(selected, selection[[s]])
    }
    # This is needed due to a bug in Shiny, see https://github.com/rstudio/shiny/issues/831
    if (is.null(selected)) {
      selected = character(0)
    }
    updateCheckboxGroupInput(session, "dynamic", selected = selected)
  }, ignoreNULL = FALSE)

})

shinyApp(ui = ui, server = server)
Comments