4

Is there a way to make R continuously scan the working directory for new files (in this case, CSVs), and whenever it finds a new file has been added to the working directory, to read it and perform some (always the same) task on it, and then go back to scanning for new files, until I tell it to stop?

Crt
  • 3,691
  • 3
  • 33
  • 48
  • Set up a cron job, that would run the same script in regular intervals, or use infinite loop like: `while(TRUE){ list.files()... check if any new, if yes, then tasks}` you might want to introduce pause within while loop. – zx8754 Mar 01 '17 at 22:37
  • You can make use of this: https://github.com/bnosac/taskscheduleR – lbusett Mar 01 '17 at 22:46
  • Very similar question: answers suggest using a system API or the qtbase package. http://stackoverflow.com/questions/4780632/monitoring-for-changes-in-files-in-real-time – neilfws Mar 01 '17 at 23:20

1 Answers1

2

I would recommend putting this in a while loop.

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
}
Gregor Thomas
  • 104,719
  • 16
  • 140
  • 257
Crt
  • 3,691
  • 3
  • 33
  • 48