title: Saving Intermediate Results of a Loop In Case of "Crash"?
body: <p>I have this loop in R:</p>
  <pre><code>output = list()
  
  for (i in 1:999) 
  
  {tryCatch({
  
  {
    
      link_i&lt;-  paste0(www.some_website, i+1,  /some_extension/, i,  .com)
      
      material_i &lt;-  fromJSON(link_i)
  
  
      
     
      
     output[[i]] &lt;- material_i
  
  }
      
     }, error = function(e){})
  
  }
  </code></pre>
  <p>Currently, if this Loop &quot;crashes&quot; - for example, if this loop crashes at the 998th iteration, I lose all my progress.</p>
  <p>I have used the &quot;tryCatch&quot; statement to skip any errors that might be encountered while the loop is running. But I am interested in the following:</p>
  <ul>
  <li><p>Suppose I click the &quot;red stop sign button&quot; in the corner of my screen and interrupt my loop - is there some code I can add to this loop that result in the &quot;results&quot; being saved up until the point that the loop was interrupted?</p>
  </li>
  <li><p>Suppose my computer shuts off (e.g. runs out of battery) - is there some code that I can add to this loop that would result in my work being saved as an &quot;RDS&quot; file on the computer? For example, after every 5 minutes, the intermediate work gets saved to &quot;my documents&quot;?</p>
  </li>
  </ul>
  <p>Thank you!</p>