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?
Asked
Active
Viewed 198 times
4
-
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 Answers
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
}
![](../../users/profiles/903061.webp)
Gregor Thomas
- 104,719
- 16
- 140
- 257
![](../../users/profiles/1292323.webp)
Crt
- 3,691
- 3
- 33
- 48
-
1I made some pretty heavy edits - the loop will now only look for files with new names, you don't need a data table dependency when we don't know what OP wants to do, `//` doesn't work for comments in R, and I added in a pause. – Gregor Thomas Mar 01 '17 at 23:43
-
-