0

I have a vector with sequence of elements. I'm trying to provide two selectInput for the user to choose elements/variables from the vector, such that the element selected in the first is excluded from second selectInput.

I tried to remove the selected element based on index from the sequence of second selectInput, but the output was only one element, instead of all the remaining list of elements. I couldn't understand why. Can someone help me?

Thank you in advance.

Below is the code:

data <- c("cultivar","control", "stress")

ui <- fluidPage(
  selectInput("select1", "Select variable", choices = data),
  uiOutput("UiSelect2")
)

server <- function(input, output, session) {
  output$UiSelect2 <- renderUI({

    #remove the selected element based on index
    newData <- data[-which(data == input$select1)]

    selectInput("select2","Select another variable", choices= ifelse(isTruthy(input$select1), newData, data))
  })
}

shinyApp(ui, server)

1 Answers1

0

Is this what you want?

library(shiny)
data <- c("cultivar","control", "stress")

ui <- fluidPage(
  selectInput("select1", "Select variable", choices = data),
  uiOutput("UiSelect2")
)

server <- function(input, output, session) {
  
  output$UiSelect2 <- renderUI({
    #remove the selected element based on index
    newData <- data[!data %in%input$select1]
    selectInput("select2","Select another variable", choices = newData)
  })

}

shinyApp(ui, server)
Pork Chop
  • 28,528
  • 5
  • 63
  • 77