Closed kaipingyang closed 4 months ago
For the first part (setting the selected parameter to NULL
) the help page for updateSelectInput states in the details section that:
Any arguments with NULL values will be ignored; they will not result in any changes to the input object on the client.
This is, in fact, not a bug.
For what regards setting the selected argument to character(0)
, the input value does change on the first click. The problem is that by calling print(input$select)
in the same observer where you update its value, you don't allow the reactive session to "flush", which means (to put it in a very simple way) that the new input values are not yet registered by the server.
You can check that it succesfully updates the input by putting the print statement in an observe like this:
library(shiny)
library(glue)
ui <- fluidPage(
selectInput("select", "Select", choices = c("A", "B", "C"),selected = c("A", "B"), multiple = TRUE),
actionButton("clear", "Clear")
)
server <- function(input, output, session) {
observeEvent(input$clear, {
updateSelectInput(session, "select", selected = "")
# updateSelectInput(session, "select", selected = character(0))
print(glue("click {input$clear}"))
})
observe({
cat("selectInput value: ")
print(input$select)
})
}
shinyApp(ui, server)
Thanks for the thoughtful reply @scal-o!
@scal-o Thanks for your explanation, it helped me a lot!
Example application
I noticed that the value of selectInput changed strangely when the selected parameter of updateSelectInput was set to null, so I thought it might be a bug.
For example, when I clicked the actionButton to set the updateSelectInput parameter to selected = NULL, I found that no matter how many times I clicked, the value of input$select did not change, just the UI widget selection was cleared.
As another example, when I click the actionButton to set the updateSelectInput parameter selected = "" or character(0), I found that the value of input$select did not change for the first click, but for the second click, The value of input$select becomes NULL, as I expected.
System details
Output of
sessionInfo()
: