0

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! :)

bathyscapher
  • 1,615
  • 1
  • 13
  • 18
  • after pressing "Reset", input$var1 is not changed to 0 until the observeEvent() is completely executed. You can see this by moving the slider to a non zero number, pressing reset (the slider moves back to zero), and then pressing "Submit" – langtang Aug 12 '22 at 15:58

2 Answers2

1

I think your code is just finishing to run the observe event, and then it is changing the value of var1. Check updateSliderInput's description: "The input updater functions send a message to the client, telling it to change the settings of an input object. The messages are collected and sent after all the observers (including outputs) have finished running.".

If you look at input$var1 in another observeEvent, you can see it changed:

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))
  })

  ## Added ##
  observeEvent(input$var1, {
    print(paste0("Here is the value of input$var1",input$var1))
    rv_text2(paste("on reset var1 =", input$var1)) # Well updated now
  })

  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)
bathyscapher
  • 1,615
  • 1
  • 13
  • 18
A.FC
  • 126
  • 5
  • Hey A.FC, thank you. Although your code isn't actually helpful (`var1` shall only change on pressing an `actionButton`), you connected the crucial point to the statement in the help: the actual content of `var1` is only changed later on what seems to make it impossible to directly use `var1`'s value after applying `updateSliderValue()`... – bathyscapher Aug 16 '22 at 09:58
0

One workaround to gain the correct value of input$var1 after resetting is using another reactive placeholder variable instead that is filled with the target value:

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"),
                  verbatimTextOutput("text3")))
)

server <- function(input, output, session) {
  rv_text1 <- reactiveVal()
  rv_text2 <- reactiveVal()
  rv_text3 <- reactiveVal()

  slider_value <- reactiveVal(0)

  value_to_reset_to <- 0

  observeEvent(input$submit, {
    rv_text1(paste("on submit var1 =", input$var1))
  })

  observeEvent(input$reset, {
    updateSliderInput(session, 'var1', value = value_to_reset_to)
    rv_text2(paste("on reset var1 =", input$var1))
    slider_value(value_to_reset_to)
  })

  observeEvent(input$var1, {
    slider_value(input$var1)
    rv_text3(paste("in placeholder =", slider_value()))
  })

  output$text1 <- renderText({rv_text1()})
  output$text2 <- renderText({rv_text2()})
  output$text3 <- renderText({rv_text3()})
}

shinyApp(ui, server)
bathyscapher
  • 1,615
  • 1
  • 13
  • 18