Mickey_NC Mickey_NC - 1 month ago 16
R Question

Hierarchy of checkboxGroupInput

I have 3 different geographical levels in my ui.R : the global level (Level 1), which contains 3 sub levels (Level 2), which each level contains 3 sub levels (Level 3).

The 3 first levels of Level 3 are contained in the first level of Level 2, the next 3 levels of Level 3 are contained in the second level of Level 2,
the last 3 levels of Level 3 are contained in the third level of Level 2.

How can I get all the check boxes checked when the Level 1 check box is checked ? And if I check the 3 first levels of Level 3, how to get the first level of Level 2 checked automatically.

Here is the ui code :

library(shiny)

shinyUI(navbarPage("RP 2014",
tabPanel("Sélection",
print (h4(strong("Geographical levels"))),
fluidRow(
column(width = 3,
h5(strong("Level 1")),
checkboxInput("dynamic_L1",
label = "")
), # f. column
column(width = 3,
checkboxGroupInput("dynamic_L2",
label = h5(strong("Level 2")),
choices = list("L2_1" = "Level 2-1",
"L2_2" = "Level 2-2",
"L2_3" = "Level 2-3"))
),
column(width = 3,
checkboxGroupInput("dynamic_L3",
label = h5(strong("Level 3")),
choices = list("L3_1" = "Level 3-1",
"L3_2" = "Level 3-2",
"L3_3" = "Level 3-3",
"L3_4" = "Level 3-4",
"L3_5" = "Level 3-5",
"L3_6" = "Level 3-6",
"L3_7" = "Level 3-7",
"L3_8" = "Level 3-8",
"L3_9" = "Level 3-9"))
)
)
)
)
)


I hope my question is understandable, thank you for helping me.

GyD GyD
Answer

As @warmoverflow suggested, use observeEvent and updateCheckboxGroupInput. You should get the gist of it from the example.

Code:

library(shiny)

l2_choices <- list("L2_1"  = "Level 2-1",
                   "L2_2" = "Level 2-2",
                   "L2_3" = "Level 2-3")

l3_choices <- list("L3_1" = "Level 3-1",
                   "L3_2" = "Level 3-2",
                   "L3_3" = "Level 3-3",
                   "L3_4" = "Level 3-4",
                   "L3_5" = "Level 3-5",
                   "L3_6" = "Level 3-6",
                   "L3_7" = "Level 3-7",
                   "L3_8" = "Level 3-8",
                   "L3_9" = "Level 3-9")

lvl3_f3 <- l3_choices[1:3]
lvl2_f1 <- l2_choices[1]

ui <- shinyUI(navbarPage("RP 2014",
                         tabPanel("Sélection",
                                  print (h4(strong("Geographical levels"))),
                                  fluidRow(
                                    column(width = 3,
                                           h5(strong("Level 1")),
                                           checkboxInput("dynamic_L1",
                                                         label = "")
                                    ), # f. column
                                    column(width = 3,
                                           checkboxGroupInput("dynamic_L2",
                                                              label = h5(strong("Level 2")),
                                                              choices = l2_choices)
                                    ),
                                    column(width = 3,
                                           checkboxGroupInput("dynamic_L3",
                                                              label = h5(strong("Level 3")),
                                                              choices = l3_choices)
                                    )
                                  )
                         )
)     
)

server <- function(input, output, session){
  observeEvent(input$dynamic_L1, {
    # Get all the checkboxes checked when the Level 1 checkbox is checked
    if(input$dynamic_L1){
      updateCheckboxGroupInput(session, inputId = "dynamic_L2", selected = l2_choices)
      updateCheckboxGroupInput(session, inputId = "dynamic_L3", selected = l3_choices)
    }
  observeEvent(input$dynamic_L3, {
    # Get the first level of Level 2 checked automatically when the 3 first levels of Level 3 are checked
    if (all(lvl3_f3 %in% input$dynamic_L3)) {
      updateCheckboxGroupInput(session, inputId = "dynamic_L2", selected = c(input$dynamic_L2, lvl2_f1))
    }
  })
  })
}

shinyApp(ui, server)
Comments