Search code examples
rshinyselectinput

Dynamically update two selectInput boxes based on the others selection in R Shiny


I am developing a shiny app and have two selectInput boxes. Both of them take the same inputs and I want to update the input boxes based on the other's selection.

Basically I want to remove the selected variable on one input box not available on the drop down of the other.

vars <- c("A", "B", "C", "D", "E", "F", "G", "H")

selectInput("v1", label = "Select Variable 1", choices = vars),
selectInput("v2", label = "Select Variable 2", choices = vars)

In the above example, if a user selects the option A as "V1", I want to remove A from the options available for v2, and vice versa. I have tried doing the following but not succeeded

  observe({
    updateSelectInput(session, "v1", choices = vars)
  })

observe({
        updateSelectInput(session, "v2", choices = vars)
  })

Solution

  • You can achieve this by using each input to filter choices in other input:

    library(shiny)
    my_vars <- c("A", "B", "C", "D", "E", "F", "G", "H")
    
    
    ui <- fluidPage(
      selectInput("v1", label = "Select Variable 1", choices = my_vars, multiple = TRUE),
      selectInput("v2", label = "Select Variable 2", choices = my_vars, multiple = TRUE)
      
    )
    
    server <- function(input, output, session){
      observe({
        if(!is.null(input$v2))
          updateSelectInput(session, "v1", 
                            choices = my_vars[!(my_vars %in% input$v2)], 
                            selected = isolate(input$v1) )
      })
      
      observe({
        if(!is.null(input$v1))
          updateSelectInput(session, "v2", 
                            choices = my_vars[!(my_vars %in% input$v1)], 
                            selected = isolate(input$v2) )
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    Note the use of isolate to avoid closing the list between choices

    In case you don't want multiple selection, initialise each input with different selections:

    ui <- fluidPage(
      selectInput("v1", label = "Select Variable 1", choices = my_vars, selected = "A"),
      selectInput("v2", label = "Select Variable 2", choices = my_vars, selected = "B")
    )