Remove (or add) choose prompt using updateSelectInput()

As explained here, selectInput(..., selectize = TRUE) does not let the user clear the input field if no choose prompt is present. This works indeed without problems. However, I would like to create a selectInput(..., selectize = TRUE) with a choose prompt and later (under certain circumstances) remove the choose prompt using updateSelectInput(). In this case, is there a way to ensure that the input field may be cleared before running updateSelectInput() (i.e. before removing the choose prompt) and that it may not be cleared afterwards (i.e. after removing the choose prompt)? I tried the following code, but it lets the user clear the input field even after running updateSelectInput():

library(shiny)

ui <- fluidPage(
  selectInput("my_select", "Label",
              choices = c(
                "Choose ..." = "",
                "A1 label" = "A1",
                "B1 label" = "B1"
              )),
  verbatimTextOutput("my_out", placeholder = TRUE),
  actionButton("upd_sel", "Update selectInput")
)

server <- function(input, output, session) {
  observeEvent(input$upd_sel, {
    updateSelectInput(session, "my_select",
                      choices = c(
                        "C1 label" = "C1",
                        "D1 label" = "D1"
                      ))
  })
  output$my_out <- renderPrint({
    input$my_select
  })
}

shinyApp(ui = ui, server = server)

The "reverse" problem occurs when creating a selectInput(..., selectize = TRUE) without a choose prompt and trying to add a choose prompt using updateSelectInput():

library(shiny)

ui <- fluidPage(
  selectInput("my_select", "Label",
              choices = c(
                "A1 label" = "A1",
                "B1 label" = "B1"
              )),
  verbatimTextOutput("my_out", placeholder = TRUE),
  actionButton("upd_sel", "Update selectInput")
)

server <- function(input, output, session) {
  observeEvent(input$upd_sel, {
    updateSelectInput(session, "my_select",
                      choices = c(
                        "Choose ..." = "",
                        "C1 label" = "C1",
                        "D1 label" = "D1"
                      ))
  })
  output$my_out <- renderPrint({
    input$my_select
  })
}

shinyApp(ui = ui, server = server)

In this case, the user may not clear the input field after running updateSelectInput(), even though it may be desirable because a choose prompt is now present. (Note that right after running updateSelectInput(), the choose prompt is shown in the input field, but it is not internally selected as may be seen in the output "my_out".)

I guess these two issues have something to do with the initialization of the selectInput(..., selectize = TRUE) and that some properties of this object (including the ability to clear the input field or not) may not be changed afterwards by updateSelectInput(). But before reporting this on shiny's issue tracker on GitHub, I wanted to ask here if there is an easy way to solve these two issues (primarily the first one, i.e. removing the choose prompt) with the existing shiny version (mine is 1.4.0.2).

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.