【发布时间】:2021-03-24 12:14:13
【问题描述】:
我正在构建一个包含多个标签的应用。我想让用户在一个选项卡中选择一个选项,然后允许他们从同一选项卡中的下游选项中进行子选择。作为下一步,我希望子选择的选项显示为第二个选项卡中的输入选项,允许用户再次进行子选择(独立于第一个选项卡)。
在下面的示例中,选择 y 会导致在第一个选项卡中选择 z 时更新。我希望用户能够子选择一些 z 值(然后绘制)。我希望 z 值的子选择然后显示为第二个选项卡中的唯一选择,并让用户在第二个选项卡中进一步子选择。
到目前为止,我所拥有的是下面的代码。我的问题是:
-
z在第一个选项卡中的子选择显然不起作用 -updateCheckboxGroupInput要么取消选择所有内容(如果我没有在函数中指定select =,否则它不会让我如果我指定了select = choices,请关闭一些z值。 - 第二个选项卡中的更新和子选择似乎完全按照我的意愿工作,我不明白为什么选项卡之间的行为不同。
任何帮助将不胜感激......
library(shiny)
library(dplyr)
library(ggplot2)
df <- data.frame(x = 1:10, y = letters[1:2], z = letters[1:10])
ui <- navbarPage("App", id = "nav",
tabPanel("Tab1",
selectInput("SelectY", label = "select value of y", choices = letters[1:2]),
checkboxGroupInput("SelectZ", label = "select value of z", choices = "a", selected = "a"),
plotOutput("plot1")),
tabPanel("Tab2",
checkboxGroupInput("SelectZ2", label = "select a subset of z values selected in tab1",
choices = "a", selected = "a"),
plotOutput("plot2")))
server = function(input, output, session){
# function to update the SelectZ (in tab1) based on selection of "y",
# and update the SelectZ (in tab2) based on selection of "z" in tab1
observe({
y <- input$SelectY
choices <- df[df$y %in% y,]$z
updateCheckboxGroupInput(session, "SelectZ", choices = choices, select = choices)
choices1 <- input$SelectZ
updateCheckboxGroupInput(session, "SelectZ2", choices = choices1, select = choices1)
})
output$plot1 <- renderPlot({
df %>%
filter(y == input$SelectY, z %in% input$SelectZ) %>%
ggplot() +
geom_point(aes(x = x, y = z))
})
output$plot2 <- renderPlot({
df %>%
filter(y == input$SelectY, z %in% input$SelectZ2) %>%
ggplot() +
geom_point(aes(x = x, y = z))
})}
shinyApp(ui = ui, server = server)
【问题讨论】: