Scheduling Tasks in Base R

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 at):

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 (taskscheduleR), 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!

it must depend on whether you can guarantee always having a background R session alive to carry out task initation based on the schedule (in lieu of windows task scheduler or another OS's offering of this feature set).

Not sure if doing the scheduling in R is a good idea. Would require R to be continuously running. I would simplify your task so it can be run on command line via > Rscript my_script.R and trigger this command with Windows task scheduler.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.