Shiny: selectizeInput placeholder goes away when multiple = FALSE

Created on 4 May 2016  ·  5Comments  ·  Source: rstudio/shiny

When a selectizeInput with a placeholder, choices = NULL, selected = NULL, and multiple = FALSE is updated to have a set of choices with length > 0, the placeholder disappears and the first element of choices becomes the selected value. I want the placeholder to remain and selected = NULL, since only the choices were updated, not the selected argument.

However, if multiple = TRUE, when choices are added the placeholder remains and selected remains NULL.

Below is a minimal example: when the UI loads, the selectizeInput has a placeholder, choices = NULL and multiple = FALSE. When you check the box, the server updates the choices to be c("var1", "var2", "var3", but does NOT update selected, yet the placeholder disappears and "var1" is selected by default. This happens even if I specify selected = NULL in the updateSelectizeInput call.

Is this a bug or intended? Is there a way to keep a placeholder and nothing selected with multiple = FALSE?

library(shiny)

ui <- shinyUI(fluidPage(

  # Application title
  titlePanel("selectizeInput demo"),

  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      selectizeInput(
        inputId = "variable",
        label = h3("Treatment variable"),
        choices = NULL,
        multiple = FALSE,
        selected = NULL,
        options = list(placeholder = "This is a placeholder")
      ),

      checkboxInput(
        inputId = "add_choices",
        label   = "Add choices",
        value   = FALSE)
    ),
    mainPanel = NULL
  )
))

server <- shinyServer(function(input, output, session) {

  observe({
    if (input$add_choices) {

      updateSelectizeInput(
        session = session,
        inputId = "variable",
        choices = paste0("var", 1:3),
        selected = NULL)
    }
  })

})

shinyApp(ui = ui, server = server)

Most helpful comment

It looks like this can be solved in a somewhat-backward way by setting multiple = TRUE but including the selectize.js option maxItems = 1. The placeholder remains, but the number of items is still capped at 1, as if effectively multiple = FALSE.

selectizeInput(
        inputId = "variable",
        label = h3("Treatment variable"),
        choices = NULL,
        multiple = TRUE,
        selected = NULL,
        options = list(
            placeholder = "This is a placeholder",
            maxItems = 1)
      )

All 5 comments

It looks like this can be solved in a somewhat-backward way by setting multiple = TRUE but including the selectize.js option maxItems = 1. The placeholder remains, but the number of items is still capped at 1, as if effectively multiple = FALSE.

selectizeInput(
        inputId = "variable",
        label = h3("Treatment variable"),
        choices = NULL,
        multiple = TRUE,
        selected = NULL,
        options = list(
            placeholder = "This is a placeholder",
            maxItems = 1)
      )

@mdec your solutions seems like a workaround, my guess is that the developers may want to fix the underlying bug.

Yes, I'll reopen, because it sounds like it's worth fixing.

It turns out you can get the placeholder by using selected=character(0) instead of selected=NULL.

selected = character(0), multiple = FALSE,
options = list(create = TRUE, placeholder = 'choose an option')

did not work for me. I had to use the workaround. But the workaround does not allow me to implement typing with only one option when updating the UI with

updateSelectizeInput

A "clean" solution would be preferable IMO

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Stophface picture Stophface  ·  3Comments

hudon picture hudon  ·  4Comments

daattali picture daattali  ·  5Comments

wch picture wch  ·  3Comments

snowman55 picture snowman55  ·  5Comments