How do I prevent redundant updating in R Shiny?

input_holder <- reactiveValues(
  a = 7
)
observeEvent(input$a_button, {
  # does some computation that arrives at some_number
  input_holder$a <- some_number
})
observeEvent(input$slider_name, {
  # should do something only if not caused by input$a_button
})
output$some_output <- renderUI({
  sliderInput('slider_name', 'some_label', max=10, min=1, value=input_holder$a)
})

So the above is the model of my code.

So here is how I think the order of events will happen if a_button is pressed.

  1. The observeEvent that observes a_button will get triggered and sets input_holder$a to some_number.

  2. This will cause the slider_name to get rendered with this new value.

  3. This then will trigger the observeEvent that listens to slider_name and execute the block of code inside it.

But that is not the behavior that I want. I only want the observeEvent that listens to slider_name to execute its block of code if the slider_name was altered by the user and not as a side effect by some other Event.

Upvotes: 0

Views: 204

Answers (1)

Paul
Paul

Reputation: 2959

I think you're looking for updateSliderInput, which will update the slider without re-rendering:

observeEvent(input$a_button, {
  # does some computation that arrives at some_number
  # input_holder$a <- some_number # do you need to store the value? If you do, I think you should use eventReactive
  updateSliderInput(session, "slider_name", value = some_number)
})

Upvotes: 2

Related Questions