title: R Shiny: How to temporarily disable reactivity?
body: <p>I am building a UI containing <code>DT</code> tables and sliders (both as inputs), as well as plot outputs. The tables are used to make a selection out of several. The user can only select one cell to make a choice.</p>
  
  <p>I want the user to be able to store the setting of tables and sliders because they are quite complex. The idea is that the user can then switch back and forth between two stored settings, for example, and see how the resulting plots change. When a user restores a setting, the tables and sliders get updated, which updates the plot(s).</p>
  
  <p>The problem is that the plot is not updated once, but usually twice. It seems that there is a delay somewhere in the logic, causing Shiny to first react to the update of the sliders, then to the update of the tables, so that the plot is re-plotted in two steps. This is very annoying for two reasons: (1) it causes the calculation to re-run twice, making the app react twice as slow and (2) it's impossible to see the changes directly in the plot because the original plot is first replaced by an intermediate plot which has no meaning to the user.</p>
  
  <p>To illustrate the problem, I created this minimum working example, where I reduced complexity to just one table and one slider. I added a 3 second <code>Sys.sleep</code> to simulate a long calculation because obviously one would not see the problem otherwise:</p>
  
  <pre><code>library(shiny)
  library(DT)
  
  ui &lt;- fluidPage(
    sidebarLayout(
      sidebarPanel(
        actionButton("button", "Preset"),
        # No problem with selectInput:
        # selectInput("select", "x", choices = names(iris)[1:4], selected = "Sepal.Length"),
        DT::dataTableOutput("table"),
        sliderInput("slider", "bins", min = 1, max = 50, value = 30)
      ),
      mainPanel(
        plotOutput("distPlot")
      )
    )
  )
  
  server &lt;- function(input, output, session) {
    observeEvent(input$button, {
      # updateSelectInput(session, "select", selected =  "Petal.Width")
      selectRows(DT::dataTableProxy("table"), 4)
      updateSliderInput(session, "slider", value = 15)
    })
  
    output$table &lt;- DT::renderDataTable(
      DT::datatable(
        data.frame(x = names(iris)[1:4]),
        rownames = FALSE,
        selection = "single",
        options = list(searching = FALSE, paging = FALSE, info = FALSE, ordering = FALSE)
      )
    )
  
    output$distPlot &lt;- renderPlot({
      req(input$table_rows_selected)
      # x &lt;- iris[[input$select]]
      x &lt;- iris[[input$table_rows_selected]]
      bins &lt;- seq(min(x), max(x), length.out = input$slider + 1)
      # Simulate long calculation:
      Sys.sleep(3)
      hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>Clicking first on the cell "Sepal.Length" in the table, then on the button "Preset" will load the preset and demonstrate the problem.</p>
  
  <p>It seems that this is a timing issue/<a href="https://en.wikipedia.org/wiki/Race_condition" rel="noreferrer">race condition</a>, because sometimes, it works OK and the plot is updated only once (only in the minimal example, not the actual app). Usually the first time after starting the app. But in that case, just click on "Sepal.Length" again and change the slider position, then click on the "Preset" button and usually the plot will update twice.</p>
  
  <p>I noticed that the problem does not appear when I replace the table with a <code>selectInput</code>. But the tables have a certain meaning: they stand for morphological fields (see package <a href="https://github.com/sgrubsmyon/morphr" rel="noreferrer"><code>morphr</code></a>), so I'd rather stick with tables to have the right appearance.</p>
  
  <p>I could obviuously also disable reactivity using <code>isolate()</code> as suggested here: <a href="https://stackoverflow.com/questions/31594052/r-shiny-how-to-prevent-duplicate-plot-update-with-nested-selectors">R Shiny: how to prevent duplicate plot update with nested selectors?</a> and then e.g. introduce a button "Update plot". But I would prefer to keep the app reactive to changes in the sliders and tables, because that's a very useful user experience and one reason for me to use Shiny instead of PHP/python/etc.</p>
  
  <p>My first idea to solve the problem was to introduce a reactive value:</p>
  
  <pre><code>server &lt;- function(input, output, session) {
    updating &lt;- reactiveVal(FALSE)
    # ...
  }
  </code></pre>
  
  <p>then change the value before and after the updates to the inputs:</p>
  
  <pre><code>observeEvent(input$button, {
    updating(TRUE)
    selectRows(DT::dataTableProxy("table"), 4)
    updateSliderInput(session, "slider", value = 15)
    updating(FALSE)
  })
  </code></pre>
  
  <p>and add an <code>if</code> statement in the <code>renderPlot()</code> code, e.g. with <code>validate</code>:</p>
  
  <pre><code>output$distPlot &lt;- renderPlot({
    validate(need(!updating(), ""))
    # ...
  })    
  </code></pre>
  
  <p>But that has no effect, because the entire code in the <code>observeEvent(input$button)</code> runs first, setting <code>updating</code> to <code>TRUE</code> and immediately back to <code>FALSE</code>. But the code inside <code>renderPlot()</code> is executed later (after the invalidation has occurred) and <code>updating</code> is always <code>FALSE</code> when it runs.</p>
  
  <p>So, at the moment I have few ideas how to solve this. It would be best if one could somehow disable reactivity for the plot, then update the inputs, enable reactivity again and trigger a plot update programmatically. But is this possible?</p>
  
  <p>Any other ideas for a workaround?</p>