首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >checkboxGroupInput层次结构

checkboxGroupInput层次结构
EN

Stack Overflow用户
提问于 2016-10-09 02:03:38
回答 1查看 676关注 0票数 0

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

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

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

下面是ui代码:

代码语言:javascript
运行
复制
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"))
                           )
                           )
                      )
                )     
  )

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-09 17:24:19

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

代码:

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39939237

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档