Home > Back-end >  Saving the Output of a Loop Every "X" Seconds
Saving the Output of a Loop Every "X" Seconds

Time:09-26

I want to make a loop that saves the results of the loop to your computer every "x" seconds. For example, consider the following loop:

my_list <- list()

for (i in 1:10000000) {
  a_i <- rnorm(1, 100, 100)
  my_list[[i]] <- a_i
  saveRDS(my_list, "my_list.RDS")
}

I want to make it so that the "saveRDS" command is executed every 33 seconds, thus overwriting the previous version of the file.

I know that the "Sys.time()" function can be used to record time in R and "Sys.sleep()" can be used to pause time - but I am not sure how it can be used to use these functions together to perform this task.

Can someone please show me how to do this?

Thank you!

CodePudding user response:

@ Jay.sf : I made this small adjustment based on your answer and everything seems to be working now! Thank you so much!

my_list <- list()

for (i in 1:100000000000) {
 a_i = rnorm(1,100,100)
  tm <- Sys.time()
  my_list[[i]] <- a_i
  saveRDS(my_list, "my_list.RDS")
  Sys.sleep(5)  ## actually 33 secs
}

CodePudding user response:

If you want the loop to do stuff every x seconds, set Sys.sleep at the end, i.e. before the next iteration starts. The time might get longer, if the "stuff" to be done needs long time. Here my proposal:

my_list <- list()

for (i in 1:5) {
  tm <- Sys.time()
  ## do stuff
  my_list[[i]] <- tm
  saveRDS(my_list, "my_list.RDS")
  Sys.sleep(3)  ## actually 33 secs
}

readRDS("my_list.RDS")
# [[1]]
# [1] "2022-09-25 19:37:26 CEST"
# 
# [[2]]
# [1] "2022-09-25 19:37:29 CEST"
# 
# [[3]]
# [1] "2022-09-25 19:37:32 CEST"
# 
# [[4]]
# [1] "2022-09-25 19:37:35 CEST"
# 
# [[5]]
# [1] "2022-09-25 19:37:38 CEST"
  • Related