Save a file in R every hour

Deadly 提交于 2019-12-08 02:56:17

问题


I have a continous process that collects data, and I want to write the data that were collected every hour. Simply, how can I conditionally save data every hour to an .Rdata file.

For context, I collect data in a list, wish to save the list object to an hourly file, remove the list, and rebuild it.

I tried the code below but it did not work:

 if (identical(format(Sys.time(), "%M:%S"), "00:00")) {
      save(twt, file=fname_r)
 }

Any help will be much appreciated.


回答1:


You may be going about this in the wrong way. Not everything is a job for R (given that R really is single-threaded), and scheduling has always been a key operating system task. Use cron, or if you're on that market-leading OS from the Northwest, look into its scheduling options. Then setup a trivial Rscript file.

Have the continuous collection process run to collect, and to dump results somewhere, either in ascii or binary. Then have an hourly job collecting the most recent dumps. That can well be done in R once you figured the scheduling out.

As for the narrower question of deciding whether an hour has passed, use something like

then <- Sys.time()
# ... stuff happens ...
now <- Sys.time()
if (as.numeric(difftime(now, then, unit="mins") > 60) {
   # .. do stuff
}



回答2:


To do the scheduling in R you could use the tclTaskSchedule function in the tcltk2 package. You tell it how lond to wait between running the tasks, the task to run (an expression/function) and to redo the task, then in the background it will run the task on a regular basis. Just be careful that you don't have 2 processes interfering with each other. If your task to save the object runs at the same time as something else is updating the same object then there is a chance that only part of the object will be saved or that what is saved is giberish. So you need some way to check if the data object is complete before saving it.



来源:https://stackoverflow.com/questions/8841614/save-a-file-in-r-every-hour

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!