我有一个闪亮的应用程序,其中有多个输入属于层次结构(A - > B - > C)。当用户选择A时 - 它应该影响B和C中的选项。但是,用户只能选择B(然后它也应该影响其他输入。如果没有选择 - 所有选项都应该可用。我该如何实现它?
答案 0 :(得分:2)
同意您需要更多信息,而observe
模式可能非常合适。如果您在界面中需要更多控制和动态,则可以使用带有renderUI
的动态UI:
library(shiny)
choices_A <- list("Choice A" = 1, "Choice B" = 2, "Choice C" = 3)
choices_B <- list("Choice B1" = 4, "Choice B2" = 5, "Choice B3" = 6)
choices_C <- list("Choice C1" = 7, "Choice C2" = 8, "Choice C3" = 9)
shinyApp(
ui = fluidPage(
titlePanel("Cascading selects"),
fluidRow(
column(3, wellPanel(
selectInput("select_A", label = "Select A",
choices = choices_A)
)),
column(3, wellPanel(
uiOutput("ui")
)),
column(3, wellPanel(
tags$p("First Input: "),
verbatimTextOutput("value"),
tags$p("Dynamic Input: "),
verbatimTextOutput("dynamic_value")
))
)
),
server = function(input, output) {
output$ui <- renderUI({
if (is.null(input$select_A))
return()
switch(input$select_A,
"1" = selectInput("dynamic", label = "Select A2",
choices = choices_A),
"2" = selectInput("dynamic", label = "Select B",
choices = choices_B),
"3" = selectInput("dynamic", label = "Select C",
choices = choices_C)
)
})
output$value <- renderPrint({ input$select_A })
output$dynamic_value <- renderPrint({ input$dynamic })
}
)
答案 1 :(得分:1)
这里没有足够的信息。但是,根据您的描述,您可以尝试从这里开始。
shinyServer(function(input, output, session) {
observe({
a_option <- input$a_option
b_option <- input$b_option
if (a_option == "XXX") {
updateSelectInput(session, "B", choices = b_options)
updateSelectInput(session, "C", choices = c_options)
}
if (b_option == "XXX") {
updateOtherInput(session, "input_id", ...)
}
})
})