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

Afl*_*loz 3 r shiny shinydashboard

为什么updateCheckboxGroupInput()选择超过1个复选框时不起作用。选择从上述组中的一个复选框时,下面的框应更新以显示更多特定条目。但是如果在顶部选择了多个复选框,代码就会失败。有谁知道可能是什么问题?

从这里或 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)
Run Code Online (Sandbox Code Playgroud)

Flo*_*ian 5

问题是你一直在使用else if;只要其中一个 ifs 被评估,其余的就会被跳过。基本上你是在说:如果 x 是真的,那么做 y。但是如果 x 不是真的(否则),检查 z 是否是真的,等等......所以如果 x 实际上是真的,z 永远不会被评估。我希望这个解释是有道理的。

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

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


在此处输入图片说明


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)
Run Code Online (Sandbox Code Playgroud)