Karsten W.
Karsten W.

Reputation: 18500

Empty Option for selectizeInput

My dropdown box is an optional filter. If one or more items are selected, then a filtering is performed. If nothing is selected -- no filtering.

I would like to communicate that behaviour. By default, the input box is empty. Is there a way to show some custom string instead? Also, I would like to include an option, that, when selected, clears the selection.

I tried the following, after reading this:

require(shiny)

ui <- fluidPage(
    shiny::selectizeInput(
        inputId="letters", 
        label="Restrict to Letters:",
        choices=letters, 
        selected=NULL,
        multiple = TRUE,
        options=list(
            allowEmptyOption=TRUE,
            showEmptyOptionInDropdown=TRUE,
            emptyOptionLabel="(all letters)"
        )
    )
)

server <- function(input, output){}

shinyApp(ui=ui, server=server)

}

but no "empty option" is displayed. What am I doing wrong?

Upvotes: 0

Views: 169

Answers (1)

Ifeanyi Idiaye
Ifeanyi Idiaye

Reputation: 1112

In order to be able to clear your selections, you will have to update the selectize input and activate it with an action button:

library(shiny)

ui <- fluidPage(
  shiny::selectizeInput(
    inputId="letters", 
    label="Restrict to Letters:",
    choices=letters, 
    selected=NULL,
    multiple = TRUE,
    options=list(
      allowEmptyOption=TRUE,
      showEmptyOptionInDropdown=TRUE,
      emptyOptionLabel="(all letters)"
    )
  ),
  actionButton("update","Clear")
)

server <- function(input, output,session){
  observeEvent(input$update,{
    updateSelectizeInput(inputId = "letters",choices = "",session)
  })
}

shinyApp(ui=ui, server=server)

enter image description here

Upvotes: 1

Related Questions