Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to continuously restart/loop R script

Tags:

r

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?

like image 885
crazjo Avatar asked Dec 20 '25 01:12

crazjo


2 Answers

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.

like image 134
CHP Avatar answered Dec 21 '25 16:12

CHP


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.

like image 21
Greg Snow Avatar answered Dec 21 '25 15:12

Greg Snow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!