简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM