Hi all,
I have a piece of Shiny code where one observeEvent changes the choices of a selectInput that's monitored by another observeEvent. The issue is that if the default value of the new choices is the same as the currently selected one from the old one, the second observeEvent is not triggered.
Here is a reprex:
library(shiny)
ui <- fluidPage(
selectInput("input1", "Input 1", choices = 1:5),
selectInput("input2", "Input 2", choices = NULL),
textOutput("output1")
)
server <- function(input, output, session) {
observeEvent(input$input1, {
updateSelectInput(session, "input2",
choices = c(LETTERS[1:sample(2:6, 1)]))
})
observeEvent(input$input2, {
showModal(modalDialog("Something changed in input 2"))
}, ignoreInit = T)
}
shinyApp(ui, server)
When the app first loads, the modal pops up. But after that, changing the choice of Input1 will update the values of Input2, but it won't trigger the code inside it (because the choice hasn't changed). In my real example, the same option for 2 can have different results depending on what's selected in 1.
I first tried triggering the second observeEvent with both input$input1 and input$input2. This does do the job, but in case the new and old value of input2 would be different, this code would trigger the observeEvent twice...
I'm sure I'm missing something obvious, but I just can't see it
Thanks,
PJ