3

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?

2 Answers 2

3

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 })
  }
)

cascading selects

Sign up to request clarification or add additional context in comments.

2 Comments

Jason +@Boxuan - Do I need to define all the possible cases? (if user chooses A it should affect values in B and C, then if he choose C it should also affect B etc.) Also - what is the main difference for using renderUI / observe?
@UserIL It depends - you have not really provided enough information for anyone to adequately answer your question. It's easier for others to help you if you show code that you are trying and illustrate your expected output. For differences between renderUI and observe see: this and this.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.