crazjo
crazjo

Reputation: 555

How to continuously restart/loop R script

I want an R script to continuously run and check for files in a folder and do something with those files.

The code simply checks for a file, then moves the file to somewhere else and renames it, deleting the old file (in reality it's a bit more elabore than this).

If I run the script it works fine, however I want R to automatically detect for the files. In other words, is there a way to have R run the script continuously so that I don't have to run the script if I put files in that folder?

Upvotes: 3

Views: 3185

Answers (2)

Greg Snow
Greg Snow

Reputation: 49640

You can use the function tclTaskSchedule from the tcltk2 package to schedule a function or expression to run on a regular interval. You can have multiple such tasks scheduled and still work in the R session (just be careful not to modify something that the scheduled task could also modify or you can get unpredictable results).

Though an OS based solution that runs a given rscript may still be a better approach.

Upvotes: 1

CHP
CHP

Reputation: 17189

In pure R you just need an infinite repeat loop...

repeat {
  print('Checking files')
  # Your code to do file manipulation

  Sys.sleep(time=5)  # to stop execution for 5 sec

}

However there may be better tools suitable to do this kind of file manipulation depending on your OS.

Upvotes: 8

Related Questions