Mickey_NC
Mickey_NC

Reputation: 269

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.

EDIT :

I have understood that dynamic_choices is for labeling, so I have included my final labels but it doesn't work. I'm so disappointed to realize that I have not understood. Here is what I've done :

library(shiny)
library(stringr)

dynamic_choices = c("01. Bélep", "02. Boulouparis", "03. Bourail",
                    "04. Canala", "05. Dumbéa", "06. Farino",
                    "07. Hienghène", "08. Houaïlou", "09. Ile des Pins", 
                    "10. Kaala Gomen", "11. Koné", "12. Koumac",
                    "13. La Foa", "14. Lifou", "15. Maré",
                    "16. Moindou", "17. Mont Dore", "18. Nouméa",
                    "19. Ouégoa", "20. Ouvéa", "21. Païta",
                    "22. Poindimié", "23. Ponérihouen", "24. Pouébo",
                    "25. Pouembout", "26. Poum", "27. Poya",
                    "28. Sarraméa", "29. Thio", "30. Touho", "31. Voh",
                    "32. Yaté", "33. Kouaoua")

selection = list("Province Nord"=c("01. Bélep", "04. Canala",
                "07. Hienghène", "08. Houaïlou", "10. Kaala Gomen",
                "11. Koné", "12. Koumac", "19. Ouégoa", "22. Poindimié",
                "23. Ponérihouen", "24. Pouébo", "25. Pouembout",
                "26. Poum", "27. Poya", "30. Touho", "31. Voh",
                "33. Kouaoua"),
                  "Province Sud"=c("02. Boulouparis", "03. Bourail",
                "05. Dumbéa", "06. Farino", "09. Ile des Pins",
                "13. La Foa", "16. Moindou", "17. Mont Dore", 
                "18. Nouméa", "21. Païta", "28. Sarraméa",
                "29. Thio", "32. Yaté"),
                   "Province des Iles Loyauté"=c("14. Lifou",
                "15. Maré","20. Ouvéa"))

ui <- shinyUI(fluidPage(

   titlePanel("Update CheckboxGroupInput"),

   sidebarLayout(
    sidebarPanel(
      checkboxGroupInput("dynamic_provinces", "Provinces", c("Province 
                    Nord", "Province Sud", "Province des Iles Loyauté")),
      checkboxGroupInput("dynamic", "Communes", dynamic_choices)
    ),

    mainPanel(
   )
 )
))

And the Server.R code :

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

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

    for (s in input$dynamic_provinces) {
      selected = c(selected, selection[[s]])
    }
       if (is.null(selected)) {
      selected = character(0)
    }
     updateCheckboxGroupInput(session, "dynamic", selected = selected)
   }, ignoreNULL = FALSE)

})

Thank you very much.

Upvotes: 1

Views: 1608

Answers (1)

Xiongbing Jin
Xiongbing Jin

Reputation: 12087

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)

Upvotes: 1

Related Questions