I want to implement an observer that reacts to events triggered by both user and update function (selectInput
) using bindEvent
. While this works fine in most cases, updateSelectInput
does not trigger an event when the updated choices do not differ from the previous choices, i.e. changing the choices from c(1, 2, 3)
to c(1, 2, 3)
does not lead to an event trigger.
Here's a reprex with comments:
library(shiny)
library(magrittr)
ui <- fluidPage(
selectInput("pick1", label = "Pick input", choices = c(1, 2, 3)),
actionButton("button1", "Update 1"),
selectInput("pick2", label = "Pick input", choices = c(1, 2, 3)),
actionButton("button2", "Update 2")
)
server <- function(input, output, session) {
# Notice that the updated choices are the same as the default ones, so
# nothing changes and no event is triggered.
observe({
updateSelectInput(inputId = "pick1", choices = c(1, 2, 3))
}) %>%
bindEvent(input$button1)
# This only executes when the user explicitly changes the selection as the
# updater does not trigger input$pick1
observe({
message("Something has triggered pick1 (probably not the updater)")
}) %>%
bindEvent(input$pick1)
# When the choices differ, the updater does trigger an event
observe({
updateSelectInput(inputId = "pick2", choices = sample(1:100, size = 3))
}) %>%
bindEvent(input$button2)
# This executes when the user changes the selection AND when the selection
# is updated on the server side
observe({
message("Something has triggered pick2 (possibly the updater)")
}) %>%
bindEvent(input$pick2)
}
runApp(list(ui = ui, server = server))
This makes sense, but it leads to an inconsistency when the updated choices are dynamically generated and happen to be the same as before. Is there any way to force an event trigger even when nothing actually changes? I have already tried shinyWidgets::pickerInput()
with shinyWidgets::updatePickerInput(..., clearOptions = TRUE)
, but this does not seem to do anything in my favor.