我有多个属于层次结构(A-> B-> C)的输入的闪亮应用程序。当用户选择 A - 它应该影响 B 和 C 中的选项。但是,用户只能选择 B(然后它也应该影响其他输入。如果没有选择 - 所有选项都应该可用。我该如何实现它?
问问题
5340 次
2 回答
3
同意您需要更多信息,并且该observe
模式可能很合适。如果您需要界面中的更多控制和动态,您可以使用动态 UI renderUI
:
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 })
}
)
于 2015-11-19T16:13:12.820 回答
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", ...)
}
})
})
于 2015-11-19T16:07:13.060 回答