I have this loop in R:
results = list()
for (i in 1:999)
{tryCatch({
{
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!