Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
How to Schedule R Scripts

How to Schedule R Scripts

Posted May 20, 2020 at 10:47 am
Andrew Treadway

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 Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.