How to Schedule R Scripts



Blogger,, and Senior Data Scientist

Running R with taskscheduleR and cronR

In a previous post, we talked about how to run R from the Windows Task Scheduler. This article will talk about two additional approaches to schedule R scripts, including using the taskscheduleR package on Windows and the cronR package for Linux. For scheduling Python code, check out this post.

Schedule R scripts with taskscheduleR

Let’s install taskscheduleR using the install.packages command.


Next, we just need to load the package to get started.


Creating a sample R script to run automatically

Before we do any scheduling, we need to first create a script. We’ll save the code below in a file called “create_file.txt”. This script will randomly generate a collection of integers and write them out to a file.

nums <- sample(10000, 100)

write(nums, “sample_nums.txt”)

Using the taskscheduler_create function

Next, in order to schedule the script to run automatically, we need to use the taskscheduler_create function. This function takes several arguments, which can be seen below.

taskscheduler_create(taskname = “test_run”, rscript = “/path/to/file/create_file.R”,
schedule = “ONCE”, starttime = format(Sys.time() + 50, “%H:%M”))

Firstly, we need to give a name to the task we want to create. In this case, we’ll just call our task “test_run”. Next, we need to specify the R script we want to automatically run. Third, we add the schedule parameter, which denotes how frequently we want to run this script. There are several options here, including WEEKLY, DAILY, MONTHLY, HOURLY, and MINUTE. For example, if we want our script to run every day, we would modify our function call like this:

taskscheduler_create(taskname = “test_run”, rscript = “/path/to/file/create_file.R”,
schedule = “DAILY”, starttime = format(Sys.time() + 50, “%H:%M”))

The other parameter we need to select is start time. In our examples, we’re setting the task to start in 50 seconds from the current time.

In addition to these arguments, taskscheduler_create also has a parameter called “modifier”. This allows us to modify the schedule frequency. For example, what if we want to run the task every 2 hours? In this case, we would just set modifier = 2.

taskscheduler_create(taskname = “test_run”, rscript = “/path/to/file/create_file.R”,
schedule = “HOURLY”, starttime = format(Sys.time() + 50, “%H:%M”), modifier = 2)

Similarly, we could run our script every 10 minutes using the code below, with a modifier of 10.

taskscheduler_create(taskname = “test_run”, rscript = “/path/to/file/create_file.R”,
schedule = “MINUTE”, starttime = format(Sys.time() + 50, “%H:%M”), modifier = 10)

Passing arguments to the Task Scheduler

We can pass arguments to the scheduled task using the “rscript_args” parameter.

taskscheduler_create(taskname = “test_run”, rscript = “/path/to/file/create_file.R”,
schedule = “MINUTE”, starttime = format(Sys.time() + 50, “%H:%M”), modifier = 10,
rscript_args = c(“10”, “test”, “this”))

Listing the tasks in the scheduler

What if we want to look at all the tasks currently in the task scheduler? There’s a quick method for that called taskscheduler_ls.


Running this function returns a data frame of all the tasks currently in the task scheduler.

Passing arguments to cronR

Running an R script with command line arguments is a common need. This can be handled with cronR using the “rscript_args” parameter in the cron_rscript function.

cmd <- cron_rscript("/path/to/file/create_file.R", rscript_args = c("10", "test", "this"))

cron_add(command = cmd, frequency = ‘/30 * * * *’, at = “15:00” , id = ‘test_linux_run’, description = “testing linux scheduler”)

Visit to download ready-to-use code, and read the rest of the article:

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from and is being posted with permission from The views expressed in this material are solely those of the author and/or and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.