每小时在 R 中保存一个文件 [英] Save a file in R every hour

查看:27
本文介绍了每小时在 R 中保存一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个收集数据的连续过程,我想写入每小时收集的数据.简单地说,我如何有条件地每小时将数据保存到 .Rdata 文件中.

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)
 }

任何帮助将不胜感激.

推荐答案

您可能以错误的方式处理此问题.并非一切都是 R 的工作(鉴于 R 确实是单线程的),并且调度一直是操作系统的一项关键任务.使用 cron,或者如果您使用的是西北地区市场领先的操作系统,请查看其调度选项.然后设置一个简单的 Rscript 文件.

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.

运行连续收集过程以收集结果并将结果以 ascii 或二进制格式转储到某处.然后有一个小时的工作来收集最近的转储.一旦您确定了调度,这可以在 R 中很好地完成.

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
}

这篇关于每小时在 R 中保存一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆