Here's a minimal reprex without the module. It works as expected — the value of x is only reset after you modify y:
ui <- fluidPage(
numericInput("x", "x", value = 9999, step = 1),
selectInput("y", "y", c(2020:2050))
)
server <- function(input, output, session) {
y <- reactive(input$y)
observeEvent(y(), {
updateNumericInput(session, "x", value = 0)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
So what's different with that app and the module that fails? The use of dynamic UI and a module. What happens if I use renderUI():
ui <- fluidPage(
uiOutput("ui")
)
server <- function(input, output, session) {
output$ui <- renderUI({
list(
numericInput("x", "x", value = 9999, step = 1),
selectInput("y", "y", c(2020:2050))
)
})
y <- reactive(input$y)
observeEvent(y(), {
updateNumericInput(session, "x", value = 0)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)
It again fails — and that makes me realise why. The initial value of input$y is going to be NULL, because when the app loads there is no y input, so it gets a default value ofy. So we need some way to make sure that the first time that the observeEvent() is called is after renderUI() has generated the call. I think the easiest way to do that is to use req():
ui <- fluidPage(
uiOutput("ui")
)
server <- function(input, output, session) {
output$ui <- renderUI({
list(
numericInput("x", "x", value = 9999, step = 1),
selectInput("y", "y", c(2020:2050))
)
})
y <- reactive({
req(input$y)
input$y
})
observeEvent(y(), {
updateNumericInput(session, "x", value = 0)
}, ignoreInit = TRUE)
}
shinyApp(ui, server)