Continuous scanning of new files in the working directory R

Is there a way to make R continuously check the working directory for new files (in this case, CSV) and whenever it finds a new file, it was added to the working directory to read it and execute some (always the same) on it, and then go back to scanning for new files until I stop?

+4
source share
1 answer

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
}
+2
source

Source: https://habr.com/ru/post/1671193/


All Articles