shantanu garg
shantanu garg

Reputation: 31

Updatepickerinput with change in pickerinput in Shiny

I want to update the Pickerinput with change in another PickerInput.How can I do it in server side in Shiny?

Upvotes: 0

Views: 2657

Answers (1)

Artem
Artem

Reputation: 3414

You could use observeEvent function at the server side to monitor the status of pickerInput #1 then use updatePickerInput function to update pickerInput #2.

Please see the code below, which takes the first letter in pickerInput #1 and chooses the content of pickerInput #2 accordingly:

library("shiny")
library("shinyWidgets")

ui <- fluidPage(
  tags$h2("Update pickerInput"),

  fluidRow(
    column(
      width = 5, offset = 1,
      pickerInput(
        inputId = "p1",
        label = "Starting Letters",
        choices = LETTERS
      )
    ),
    column(
      width = 5,
      pickerInput(
        inputId = "p2",
        label = "Names of Cars",
        choices = ""
      )
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$p1, {
    updatePickerInput(session = session, inputId = "p2",
                      choices = grep(paste0("^",input$p1), rownames(mtcars), value = TRUE))

  }, ignoreInit = TRUE)
}

shinyApp(ui = ui, server = server)
}

Output: example

Upvotes: 4

Related Questions