简体   繁体   中英

Shiny updateSelectInput for multiple inputs

I have shiny app with multiple inputs that belongs to hierarchy (A -> B -> C). When user selects A - it should affect the options in B and C. However, the user can choose only B (and then it should affect other inputs as well. If nothing selected - all options should be available. How can I implement it?

Agreed that you need more information and the observe pattern could be a good fit. If you need more control and dynamics in the interface, you could use a dynamic UI with 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 })
  }
)

级联选择

There is not enough information here. However, from what you described, you could try starting from here.

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", ...)
    }
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM