Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

selectizeInput placeholder goes away when multiple = FALSE #1182

Closed
mdec opened this issue May 4, 2016 · 7 comments
Closed

selectizeInput placeholder goes away when multiple = FALSE #1182

mdec opened this issue May 4, 2016 · 7 comments
Assignees

Comments

@mdec
Copy link

mdec commented May 4, 2016

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)
@mdec
Copy link
Author

mdec commented May 4, 2016

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 mdec closed this as completed May 4, 2016
@ignacio82
Copy link

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

@wch
Copy link
Collaborator

wch commented May 4, 2016

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

@wch
Copy link
Collaborator

wch commented Aug 9, 2016

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

@wch wch closed this as completed in 1d453b6 Aug 12, 2016
@wch wch removed the targeted label Aug 12, 2016
budsonjelmont added a commit to budsonjelmont/pfcmap_expr_browser that referenced this issue Nov 27, 2019
@AnimeSana
Copy link

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

@wish1832
Copy link

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

@AnimeSana Agreed, I'm having the exact same issue when attempting to add a placeholder using updateSelectizeInput().

@le-raman
Copy link

Any update on this? The annoying part is that emptying the selection leaves you with "" instead of NULL, irrespective of the the placeholder workaround. selectInput() doesn't display this behavior/bug, but server-side processing is required by my application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants