【问题标题】:Constantly scanning for new files in R working directory [duplicate]不断扫描R工作目录中的新文件[重复]
【发布时间】:2017-07-21 11:11:58
【问题描述】:

有没有办法让 R 连续扫描工作目录中的新文件(在本例中为 CSV),并且每当它发现新文件已添加到工作目录时,读取它并执行一些(总是同样)任务,然后返回扫描新文件,直到我告诉它停止?

【问题讨论】:

  • 设置一个 cron 作业,它会定期运行相同的脚本,或者使用无限循环,例如:while(TRUE){ list.files()... check if any new, if yes, then tasks} 你可能想在 while 循环中引入暂停。
  • 你可以使用这个:github.com/bnosac/taskscheduleR
  • 非常相似的问题:答案建议使用系统 API 或 qtbase 包。 stackoverflow.com/questions/4780632/…

标签: r automation conditional read.csv


【解决方案1】:

我建议把它放在一个while循环中。

setwd("path_you're_interested_in")
old_files <- character(0)
while(TRUE){
   new_files <- setdiff(list.files(pattern = "\\.csv$"), old_files)
   sapply(new_files, function(x) {
       # do stuff
   })
   old_files = c(old_files, new_files)
   Sys.sleep(30) # wait half minute before trying again
}

【讨论】:

  • 我做了一些非常繁重的编辑——循环现在只会查找具有新名称的文件,当我们不知道 OP 想要做什么时,您不需要数据表依赖项,@987654322 @ 对 R 中的 cmets 不起作用,我在暂停中添加了。
  • @Gregor 好点。感谢您的帮助
  • 这很好用,谢谢!
猜你喜欢
  • 2015-10-06
  • 1970-01-01
  • 2013-06-16
  • 1970-01-01
  • 2021-02-05
  • 2016-09-10
  • 2011-10-14
  • 1970-01-01
  • 2019-06-03
相关资源
最近更新 更多