varSelectInput dependent on another varSelectInput

Hello,

I have this really simple example below. Essentially, I want the second varSelectInput to only provide a list we can select from that excludes the options selected with the first varSelectInput. Thus, if mpg and cyl are selected on the first one we cannot have them as options for the second.

I am relatively sure one will have to do some server side ui rendering and likely some !!! or !! to get this to work. I just want the cleanest most intuitive solution for this problem. Thanks!


shinyApp(
  ui = fluidPage(
    varSelectInput("variable_first", "Variable selected:", data = mtcars, multiple = TRUE),
    #varSelectInput("variable_remainder", "Variables selected:", data = mtcars, multiple = TRUE),
    verbatimTextOutput("text1"),
    verbatimTextOutput("text2")
  ),
  server = function(input, output) {
    output$text1 <- renderPrint({
      input$variable_first
    })
     #output$text2 <- renderPrint({
      #input$variable_remainder
    #})   
    
  }
)
library(shiny)
shinyApp(
  ui = fluidPage(
    varSelectInput("variable_first", "Variable selected:", data = mtcars, multiple = TRUE),
    selectInput("variable_remainder", "Variables selected:",choices=names(mtcars), multiple = TRUE),
    verbatimTextOutput("text1"),
    verbatimTextOutput("text2")
  ),
  server = function(input, output,session) {
    output$text1 <- renderPrint({
      input$variable_first
    })
    
    observeEvent(input$variable_first,{
      updateSelectInput(session=session,
                           inputId = "variable_remainder",
                          choices = setdiff(names(mtcars),input$variable_first)
                           )
    })
    output$text2 <- renderPrint({
    input$variable_remainder
    })
    
  }
)
1 Like

Thanks for your reply :slight_smile: This is working

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.