I have this loop in R:
output = list()
for (i in 1:999)
{tryCatch({
{
link_i<- paste0(www.some_website, i+1, /some_extension/, i, .com)
material_i <- fromJSON(link_i)
output[[i]] <- material_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 the "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!