更新复选框组输入不适用于多个复选框
Update Checkbox Group Input Not Working for More than One Checkbox
为什么这个 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)
问题是您一直在使用 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)
为什么这个 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)
问题是您一直在使用 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)