checkboxGroupInput的层次结构

时间:2016-10-09 02:03:38

标签: r shiny

我的ui.R中有3个不同的地理级别:全局级别(级别1),其中包含3个子级别(级别2),每个级别包含3个子级别(级别3)。

第3级的3个第一级包含在第2级的第一级,第3级的第3级包含在第2级的第2级, 第3级的最后3个级别包含在第2级的第3级。

如果选中“1级”复选框,如何选中所有复选框?如果我检查3级的3个第一级,如何自动检查第2级的第2级。

这是ui代码:

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"))
                           )
                           )
                      )
                )     
  )

我希望我的问题是可以理解的,谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

正如@warmoverflow建议的那样,使用observeEventupdateCheckboxGroupInput。你应该从例子中得到它的要点。

代码:

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)