needRhelp
needRhelp

Reputation: 3138

Shiny selectizeInput set selected value with option

I have a customised selectizeInput where I specify the available choices using the options argument (to have custom rendering and searching which is omitted in the example for simplicity). Now I am stuck on the simple task of setting a selected value. From the documentation this should be possible using the items field, but I am struggling to find the right way to set this.

library(shiny)
library(purrr)

choices <- purrr::transpose(list(x = letters[1:5],
                                 y = LETTERS[1:5]))

ui <- fluidPage(
  selectizeInput("select", 'Select', choices = "",
                 selected = "",
                 options = list(
                   valueField = 'x',
                   labelField = 'y',
                   items = choices[[1]],
                   options = choices
                 ))
)

shinyApp(ui, function(input, output, session) {})

Upvotes: 1

Views: 2838

Answers (1)

Ben
Ben

Reputation: 30474

There may be a better option out there, but this seems to work. Add the following in options:

onInitialize = I('function() { this.setValue("a"); }')

And see selectize example in shiny gallery under "6. Placeholder":

https://shiny.rstudio.com/gallery/selectize-examples.html

ui <- fluidPage(
  selectizeInput("select", 'Select', choices = "",
                 selected = "",
                 options = list(
                   valueField = 'x',
                   labelField = 'y',
                   #items = choices[[1]],
                   onInitialize = I('function() { this.setValue("a"); }'),
                   options = choices
                 ))
)

Upvotes: 6

Related Questions