2

I am working with the R programming language.

I am interested in learning how to schedule jobs in R - for instance, suppose I want to run the script below every Monday at 12:00 AM:

## my_script (create some random numbers, put them in a data frame,
## and save this data frame as a "csv" file containing the date/time 
## at which the file was created):

a = rnorm(100,10,10)
b = rnorm(100,10,10)

my_data = data.frame(a,b)

write.csv(my_data, paste0("my_data", format(Sys.time(), "%d-%b-%Y %H.%M"), ".csv"))

#END

Reading on the internet, I found out two ways of doing this:

1) First Way:

Using the recently developed "taskscheduleR" package in R (https://cran.r-project.org/web/packages/taskscheduleR/vignettes/taskscheduleR.html), you can specify which scripts and at what frequency you would like them to be scheduled:

#save the above script as an R file (I am not sure what "extdata" is)
myscript <- system.file("extdata", "my_script.R", package = "taskscheduleR")

## Run every week on Saturday and Sunday at 09:10
taskscheduler_create(taskname = "myfancyscript_sunsat", rscript = myscript, 
                     schedule = "WEEKLY", starttime = "09:10", days = c('SUN', 'SAT'))

2) Second Way:

Jobs can also be scheduled using the "Windows Task Scheduler" - this can be seen over here: Scheduling R Script

My Question: Is it possible to do this ENTIRELY in Base R? That is, can this be done without directly using "Windows Task Scheduler" ?

For instance, suppose I save the above script as an "R file":

enter image description here

Using only commands in Base R, is it possible to specify how often you want to run this script and then send this script to the "task scheduler" - only using commands in Base R?

Can someone please show me how to do this?

Thanks!

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
stats_noob
  • 5,401
  • 4
  • 27
  • 83
  • 3
    You're asking if base R has the ability to interface directly with the OS's scheduling system? The only way I can think of is if you put your Windows machine in "never sleep" and have R test every minute or so to start an action. – r2evans Jan 23 '22 at 20:21
  • What kind of scheduling are you trying to do, "every hour", "every 5 minutes", "daily", or something else? – r2evans Jan 23 '22 at 21:29
  • (1) by "using only commands in base R" do you mean that you *don't* want to load/make use of the `taskscheduleR` package? (2) if not, isn't the first way you described exactly what you want (you are not directly using the WTS, rather you're calling it via R); (3) are you looking for an OS-independent solution? – Ben Bolker Jan 23 '22 at 22:41
  • @BenBolker yes, that was one of my next points ... OS-agnostic is not likely possible (in my experience, perhaps I'm missing something). I think the only "OS-independent" way this can work is to have R running 24/7 in a busy loop of sorts. – r2evans Jan 23 '22 at 22:58
  • 2
    it would be reasonably straightforward to write something that used `cron` on Linux/MacOS and the WTS on Windows ... ? – Ben Bolker Jan 23 '22 at 23:02

0 Answers0