希望这会对你有所帮助。
UI.R 文件
library(shinyBS)
library(shiny)
shinyUI(fluidPage(
# input control for first choice
selectInput("first_choice",
label = h1("First Answer a General Question"),
choices = list("select","A","B","C"),
selected = "select"
),
#collapsable panel for second choice
h1("then get into details"),
bsCollapse(
bsCollapsePanel( title = "details",
uiOutput("second_choice")
),
id = "collapser", multiple = FALSE, open = NULL
),
h2("first answer"),
h3(textOutput("first_answer")),
h2("second answer"),
h3(textOutput("second_answer"))
))
server.R 文件
library(shiny)
shinyServer(function(input, output,session) {
#retrieve selected values and render text from selection
output$first_answer <- renderText({input$first_choice})
output$second_answer <- renderText({input$dynamic})
output$second_choice <- renderUI({
switch(input$first_choice,
"A" = checkboxGroupInput("dynamic", "Dynamic",
choices = c("Aragon","Frodo"),
selected = "option2"),
"B" = checkboxGroupInput("dynamic", "Dynamic",
choices = c("Bilbo","Gandalf","Sauron"),
selected = "option2"),
"C" = checkboxGroupInput("dynamic", "Dynamic",
choices = c("Boromir","Legolas"),
selected = "option2")
)
})
#observe function in order to open the collapsable panel when the first answer is given
observe({
if (input$first_choice != "select") {
updateCollapse(session,"collapser",open = c('details'))
}
})
})
这会产生以下闪亮的应用程序:
第二个菜单仅在第一个得到答案后打开,第二个问题的选项根据第一个答案动态更改。
选定的答案分配给 first_answer 和 second_answer。
请注意折叠面板使用 shinyBS 包。
您可以在以下 Rstudio 资源中找到有关动态 UI 更改的更多信息:
http://shiny.rstudio.com/articles/dynamic-ui.html
http://shiny.rstudio.com/gallery/dynamic-ui.html