Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Saving the Output of a Loop Every "X" Seconds

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Can someone please show me how to do this?

Thank you!

>Solution :

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading