代码之家  ›  专栏  ›  技术社区  ›  Afloz

更新复选框组输入不适用于多个复选框

  •  4
  • Afloz  · 技术社区  · 7 年前

    为什么会这样 updateCheckboxGroupInput() 选中多个复选框时,代码不起作用。选中上述组中的一个复选框后,下面的框应更新以显示更具体的条目。但如果在顶部选中了多个复选框,则代码将失败。有人知道问题是什么吗?

    从此处或bitbucket复制: https://bitbucket.org/snippets/afolabicrystal/deyay5

      ui <- fluidPage(
    p("The first checkbox group controls the second"),
    checkboxGroupInput("inCheckboxGroup", "Device Class",
                       c("TV", "Mobile", "Tablet")),
    checkboxGroupInput("inCheckboxGroup2", "Device Types",
                       c("Amazon TV", "Apple TV", "Android Phone", "iPhone", "iPad", "Android Tablet")))
    
    
      server <- function(input, output, session) {
    observe({
      x <- input$inCheckboxGroup
    
    
      # Can also set the label and select items
      updateCheckboxGroupInput(session, "inCheckboxGroup2",
                               label = paste("Device Types", length(x)),
                               if ('TV' %in% x) {
                                 choices = c("Amazon TV", "Apple TV")
                               } else if("Mobile" %in% x) {
                                 choices = c("Android Phone", "iPhone")
                               } else if("Tablet" %in% x) {
                                 choices = c("iPad", "Android Tablet")
                               } else if (all(c("TV","Mobile") %in% x)) {
                                 choices = c("Amazon TV", "Apple TV", "Android Phone", "iPhone")
                               } else if (all(c("TV","Tablet") %in% x)) {
                                 choices = c("Amazon TV", "Apple TV", "iPad", "Android Tablet")
                               } else if (all(c("Mobile","Tablet") %in% x)) {
                                 choices = c("Android Phone", "iPhone", "iPad", "Android Tablet")
                               } else if (all(c("TV", "Mobile", "Tablet") %in% x)) {
                                 choices = c("Amazon TV", "Apple TV", "Android Phone", "iPhone", "iPad", "Android Tablet")
                               } else {
                                 choices = character(0)
                               },
                               selected = choices
      )
    })
    }
    shinyApp(ui, server)
    
    1 回复  |  直到 7 年前
        1
  •  5
  •   Florian    7 年前

    问题是您正在使用 else if 总是一旦对其中一个ifs进行了评估,就会跳过其余ifs。基本上你是说:如果x是真的,那么做y。但是如果x不是真的(否则),检查z是否是真的,等等。。。因此,如果x实际上为真,则永远不会计算z。我希望这个解释有道理。

    此外,只需检查三个条件(电视、手机和平板电脑),即可简化代码,如果选中复选框,则可将选项添加到向量中。

    下面给出了一个工作示例。希望这有帮助!


    enter image description here


    ui <- fluidPage(
      p("The first checkbox group controls the second"),
      checkboxGroupInput("inCheckboxGroup", "Device Class",
                         c("TV", "Mobile", "Tablet")),
      checkboxGroupInput("inCheckboxGroup2", "Device Types",
                         c())
    )
    
    
    server <- function(input, output, session) {
      observe({
        x <- input$inCheckboxGroup
    
        choices=c()
        if ('TV' %in% x) 
          choices = c(choices,c("Amazon TV", "Apple TV"))
        if("Mobile" %in% x) 
          choices =c(choices, c("Android Phone", "iPhone"))
        if("Tablet" %in% x) 
          choices = c(choices,c("iPad", "Android Tablet"))
    
        updateCheckboxGroupInput(session, "inCheckboxGroup2",
                                 label = paste("Device Types", length(x)),
                                 choices=choices,
                                 selected = choices
        )
      })
    }
    shinyApp(ui, server)