title: Reactive variables and input in R Shiny: Save everything, but not every time
body: <p>I am writing functions into my shiny App to automatically backup everything the user does in case the App crashes, power cuts out, or any other form of failure. </p>
  
  <p>My thought scenario was as follows: 
  In an app with hundreds of inputs, outputs and reactive values, I wanted to write a generic function that saves each one of them, since adding a save code line everywhere where a value changes would be too much work, and prone to lead to mistakes.  This so far has led me to observe the list of a type of reactive values:</p>
  
  <p>using <code>reactiveValuesToList(input)</code> or the same for all values coming from </p>
  
  <pre><code>values &lt;- reactiveValues(values)    
  </code></pre>
  
  <p>So, I ended up with this code: </p>
  
  <pre><code>observeEvent(reactiveValuesToList(input), { 
  ## set your output directory here to save in
  ## Shorten the list to only apply save RDS to the one that has changed.... 
    lapply(names(reactiveValuesToList(input)), function(item) {
      saveRDS(input[[item]], paste("Test", "values", item, "rds", sep = '.'))
    })
  
  })
  </code></pre>
  
  <p>This code can be plugged into any standard shiny App for testing. </p>
  
  <p>The nice thing is that this code will save anything as soon as anything in the list changes, but that is also the problem. If out of 100 variables 1 changes, this code will save all 100 of them to what ever folder the user has assigned for this.</p>
  
  <p>When the variables are all very small (like a True/False status, no problem), but my app for instance works with several dozen files of 1-50 million data points, which take about 20 second each to save. 
  This would result in a scenario where EACH change of ANY variable, results in a saving loop of many minutes if not half an hour. Ridiculous obviously. </p>
  
  <p>I considered comparing the previous <code>reactiveValuesToList</code> with the new one, item by item to determine which changed, and save that one, but the comparison takes too long as well. 
  Comparing whether 2 dataframes of ~30 milion values takes about 1 seconds on my 12GB laptop, still way too much if you have to do it for dozens. 
  With this approach, each button click anywhere in the app would result in a wait time of a minute or minutes, every single time... </p>
  
  <p>So, what i am looking for is a solution that Shiny will very quickly know what is the last changed item in the list of reactiveValue/input/outputs and only save that variable. </p>
  
  <p>A working test app that saves everything at every change. </p>
  
  <pre><code>library(shiny) 
  
  rm(list = ls(), envir = globalenv())              ## to prevent cross over from old runs
  
  ui &lt;- dashboardPage(
    dashboardHeader(title = "Dummy App"),
    dashboardSidebar(
      sidebarMenu(id = "tabs",
                  menuItem("Page", tabName = "page1", icon = icon("pie-chart"))
      ) ),
    dashboardBody(
      tabItems(
  
        tabItem(tabName = 'page1',
                fluidRow(
                  uiOutput("BatchName"),
  
                  actionButton(inputId = "button1", label = "button"),
                  br(),
                  verbatimTextOutput("testing")
                )))))
  
  
  
  server &lt;- function(input, output, session) {
    values &lt;- reactiveValues(pressed = F)
  
    output$BatchName &lt;- renderUI({ textInput(inputId ="BatchName", label = NULL , placeholder = "start") })
  
    observeEvent(input$button1, { 
    outputOptions(output, "BatchName", suspendWhenHidden = FALSE)  ## without this line updating elements on page 2 and higher doesn't work as they are suspenWhenHidden = True by default
    updateTextInput(session, inputId = "BatchName", value = "Updated") 
    values$pressed &lt;- !values$pressed
    })
  
    observeEvent(reactiveValuesToList(input), { 
      ## set your output directory here to save in
      ## Shorten the list to only apply save RDS to the one that has changed.... 
      lapply(names(reactiveValuesToList(input)), function(item) {
        print(item)
        saveRDS(input[[item]], paste("Test", "values", item, "rds", sep = '.'))
        if (values$pressed == T) { 
        output$testing &lt;- renderText({'saving'}) } else {
          output$testing &lt;- renderText({'saved?'}) }
          })
        })
    }
  shinyApp(ui, server)
  </code></pre>