Leonhardt Guass
Leonhardt Guass

Reputation: 793

Best way to interrupt, save, and continue later with a loop

Here is the thing. I have a loop that takes several days to run. I want to interrupt the loop, check progress, and continue later. Currently, I am using the following:

for (i in 1:100000) {
  Sys.sleep(i*2 + 5)
  print(i)
  write.csv(i, "i.csv")
}

I check the value of i each time and change the starting point of the loop accordingly. Are there formal and better ways to do this?

Upvotes: 2

Views: 185

Answers (1)

jay.sf
jay.sf

Reputation: 72984

You could use a while loop instead. This has the advantage, that the iterator won't start always from the beginning, but resumes at it's current value. So you should be able to interrupt the loop, check the .csv, and resume then.

i <- 1
iter <- 4
while (i <= iter) {
  cat(i, '\n')
  Sys.sleep(i)
  write.csv(i, "i.csv", row.names=FALSE)
  i <- i + 1
}
# 1 
# 2 
# 3 
# 4 

read.csv("i.csv")
#   x
# 1 4

In case you want to append a row instead of rewriting the entire .csv in each iteration, try write.table.

j <- 1
iter <- 4
while (j <= iter) {
  cat(j, '\n')
  Sys.sleep(j)
  write.table(j, file="./j.csv", append=TRUE, sep=',',
              row.names=FALSE, col.names=FALSE)
  j <- j + 1
}
# 1 
# 2 
# 3 
# 4 

read.csv("j.csv", header=FALSE)
#   V1
# 1  1
# 2  2
# 3  3
# 4  4

Upvotes: 1

Related Questions