Home > other >  How to update multiple selectizeInput with textInput?
How to update multiple selectizeInput with textInput?

Time:10-26

I'm trying to use updateSelectizeInput to update the selected options using a textInput. I've tried pasting the input but only the first value was updated. I'd like to enter "A", "B", "C" as text so that the selectizeInput also reflects these values. Right now only "A" is shown in the select input box.

Example:

enter image description here

Code:

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput("text1", label = "Type (A, B, C)")
      ),
    mainPanel(
      selectizeInput("select1", choices = c("A", "B", "C"), label = "Select", multiple = TRUE,
                    options = list(
                      delimiter = ',',
                      create = I("function(input, callback){
                                   return {
                                   value: input,
                                   text: input
                                   };
                                 }")
                      )),
     textOutput("select_output")
   )
 )
)


server <- function(input, output, session) {
 
 observeEvent(input$text1, {
   updateSelectizeInput(session, "select1", selected = paste(input$text1))
 })
 
  output$select_output <- renderText({
    input$select1
  })
  
}

shinyApp(ui, server)

CodePudding user response:

You need to pass a vector to selected in updateSelectizeInput, input$text1 is a string.

Try -

library(shiny)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput("text1", label = "Type (A, B, C)")
    ),
    mainPanel(
      selectizeInput("select1", choices = c("A", "B", "C"), label = "Select", multiple = TRUE,
                     options = list(
                       delimiter = ','
                     )),
      textOutput("select_output")
    )
  )
)


server <- function(input, output, session) {
  
  observeEvent(input$text1, {
    updateSelectizeInput(session, "select1", selected = unlist(strsplit(input$text1, ',\\s ')))
  })
  
  output$select_output <- renderText({
    input$select1
  })
  
}

shinyApp(ui, server)

enter image description here

  • Related