Saving Results of a Loop In Case it Crashes?

I have this loop in R:

results = list()

for (i in 1:999)

r_i = #### some code #####

results[[i]] <- r_i


}, error = function(e){})


Currently, if this Loop "crashes" - for example, if this loop crashes at the 998th iteration, I lose all my progress.

I have used the "tryCatch" statement to skip any errors that might be encountered while the loop is running. But I am interested in the following:

  • Suppose I click the "red stop sign button" in the corner of my screen and interrupt my loop - is there some code I can add to this loop that result in "results" being saved up until the point that the loop was interrupted?
  • 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 "RDS" file on the computer? For example, after every 5 minutes, the intermediate work gets saved to "my documents"?

Thank you!

As long as ###somecode### in your example doesn't fail on the first attempt, each successful attempt thereafter should add to the results list. In the code below, I simulate a loop "crashing" at the sixth iteration. With each successful iteration up to the crash (attempts 1-5), the output (returning "attempt_i") was added to results.

In addition, once each new item is added to results, the full list is saved to MyDocuments before moving on to the next attempt. results is removed from the environment after the "crash" and then read back in to show the first five results were saved.

results = list()

for (i in 1:10) {
 if(i == 6) {
   # force a "crash" and create an output message
   stop(paste0('This is a forced "crash" at attempt i = 6.\n\n', 
               paste0('*results* captured the prevoius ', 
                      ' elements, which include:\n',
                      paste(results, collapse = '\n')
   } else {
     # add the newest attempt to the list
     results[[i]] = paste0('attempt_', i)
     # save the object with each iteration
     save(results, file = 'MyDocuments/loop_test.RData')
#> Error in eval(expr, envir, enclos): This is a forced "crash" at attempt i = 6.
#> *results* captured the prevoius 5 elements, which include:
#> attempt_1
#> attempt_2
#> attempt_3
#> attempt_4
#> attempt_5

# remove "results" from the environment

# load what was saved for "results"
load(file = 'MyDocuments/loop_test.RData')
#> [[1]]
#> [1] "attempt_1"
#> [[2]]
#> [1] "attempt_2"
#> [[3]]
#> [1] "attempt_3"
#> [[4]]
#> [1] "attempt_4"
#> [[5]]
#> [1] "attempt_5"

Created on 2022-09-15 with reprex v2.0.2.9000

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.