In a Shiny app, I want to reset a slider to its initial value with updateSliderInput()
. Now it appears, that this happens--for the moment--only graphical instead as also in the actual value (input$var1
): after setting the slider to any other than its intitial position and pressing "reset", the slider shifts back to its initial position as expected, while the printed value of input$var1
still shows the adjusted value.
Pressing "reset" a second time, then resets input$var1
.
In the code are some message()
s to highlight the sequence of events in the terminal (code mostly borrowed from here).
library(shiny)
ui <- fluidPage(
titlePanel("Reset Slider Value"),
fluidRow(column(4,
sliderInput("var1", "", min = -100, max = 100, value = 0),
actionButton('submit', 'Submit'),
actionButton("reset", "Reset")),
column(6,
verbatimTextOutput("text1"),
verbatimTextOutput("text2")))
)
server <- function(input, output, session) {
rv_text1 <- reactiveVal()
rv_text2 <- reactiveVal()
observeEvent(input$reset, {
message("Going to update")
updateSliderInput(session, 'var1', value = 0)
message("Is updated")
rv_text2(paste("on reset var1 =", input$var1))
message(paste("reset: var1 =", input$var1))
})
observeEvent(input$submit, {
rv_text1(paste("on submit var1 =", input$var1))
print(paste0("submit: var1 =", input$var1))
})
output$text1 <- renderText({rv_text1()})
output$text2 <- renderText({rv_text2()})
}
shinyApp(ui, server)
Now, I'm puzzled. I would have expected that the content of input$var1
would change along with its graphical representation, especially if one further relies on and processes the content of the slider (as it is the case for us).
So, it would be great if someone could inform me if this is intended behaviour (and if so, why) and if I am missing something here or if this is actually a bug? Many thanks in advance! :)