shiny icon indicating copy to clipboard operation
shiny copied to clipboard

Setting argument "selected" of updateSelectInput() to nonexisting value

Open fweber144 opened this issue 5 years ago • 2 comments

When using updateSelectInput() for a selectInput() which does not have a choose prompt and setting argument selected (of updateSelectInput()) to something which does not exist among the choices of argument choices, the result is somewhat counter-intuitive in my opinion. Consider this reproducible example:

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(
                        "C1 label" = "C1",
                        "D1 label" = "D1"
                      ),
                      selected = "notExisting")
  })
  output$my_out <- renderPrint({
    input$my_select
  })
}

shinyApp(ui = ui, server = server)

When clicking the actionButton(), then the input field is cleared which should not be possible (because the selectInput() was created without a choose prompt). Furthermore, input$my_select is kept at the value before clicking the actionButton(). Wouldn't it be more intuitive (and consistent with the documentation) to have the first value from the choices of the updateSelectInput() selected (i.e. "C1" in this example)?

fweber144 avatar Jun 17 '20 14:06 fweber144

I'm facing a similar problem.

I have a selectInput() with choices that I delete one at a time and I would like to check if the input value is "" or NULL when there are no choices left in order to disable some buttons. However, when you call updateSelectInput() with choices = character(0) the, it remembers the previous input value instead of returning "" or NULL.

See for example:

library(shiny)
shinyApp(
  ui = fluidPage(
    selectInput("letter", "Select a letter", "a"),
    actionButton("btn", "Update")
  ),
  
  server = function(input, output, session) {
    observeEvent(input$btn, {
      updateSelectInput(
        session = session, inputId = "letter", 
        choices = character(0)
      )
      print(input$letter)
    })
  }
)

Which always prints "a" no matter the input is empty.

tomicapretto avatar Jun 28 '20 21:06 tomicapretto

I too am seeing this behavior which is all down to: https://github.com/rstudio/shiny/blob/8eb7b056f238689ba04a90857b0f6a091b7718c8/srcts/src/bindings/input/selectInput.ts#L193-L201 This is a particular problem for dynamically populated select boxes, (i.e. 1 select box is used to produce a filtered list for the second. Changing the former "clears" the latter but shiny server remembers the latter's previous selection)

My current solution is to use character(0) and then a snippet of shinyjs:

shinyjs::runjs(paste0("Shiny.setInputValue('", session$ns("selectInputName"), "', null)"))

avsdev-cw avatar Apr 29 '22 14:04 avsdev-cw