Automating and Scheduling R Scripts in Windows: Tutorial

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

This tutorial will teach you how to run and schedule R scripts from the command line. Even though parts of this tutorial applies for other operating systems as well, the focus will be on Windows, since it is a bit less straightforward than in other systems.

By the end of this tutorial you will have the basic knowledge of how to execute operations (including R scripts) from Windows Command Prompt using a single line of code – running complex R scripts, embedding parameters within them, and scheduling processes to run repeatedly.

Running R scripts from the command line can have a couple of advantages such as automating repeating R operations, scaling a large number of R-related processes and simplifying the execution of R scripts. In some cases you might want a server to run your R script every X hours, and in other cases it might be just more convenient to run an existing script without the need to access R or RStudio.

Preparations
First, we need to add a specific path as an environment variable in our system.
1. Go to Windows “Search”
2. Type “Edit the system environment variables”
3. Click the button “Environment Variables…” (at the bottom)
4. On the bottom pane, under “System variables”, highlight the “Path” variable and click “Edit”.
5. Click “New” and add the path of the “bin” folder of your R software. The path usually looks like: C:\Program Files\R\R-3.4.4\bin\ (it might change a bit between computers or R versions)
6. Click OK in all windows

Notes: steps 1 and 2 can also be replaced with accessing “Control Panel” -> “System” -> “Advanced”.

Start an R session
Now we are ready to start running scripts from Windows Command Prompt!
Go to Windows “Search” again, and type “Command Prompt”.
To run an R session from the command line, simply type: R

If you get the usual R starting message (“R is a free software…”), you’ve done everything right and you can quit the R console for now using the function q(save = "no")
If not, you might have missed something so please go back to the Preparations section. If you’re sure you’ve done everything properly and it’s still not working for you, please contact the author of this tutorial.

Now, to run a simple R script from the command line, all you have to do is type:
Rscript path\to\the\script.R
Try it out with a script of your choice!

Pass parameters to your script
To run a script with parameters, you would have to add some code to your R script that will “unpack” the parameters for the script to use. This is how it is done:
params <- commandArgs(trailingOnly = TRUE) # notice that params will be a character vector

first_param <- params[1]
second_param <- params[2]
# n_param <- params[n] …

print(first_param)
print(second_param)

Now, when you run the script from the command line, you should simply specify the parameters after the path to the script, separated by spaces:
Rscript path\to\the\script.R value_for_the_first_parameter value_for_the_second_parameter

Automate processes by scheduling tasks that run R scripts
The Windows equivalent of the famous cron utility is called “Schtasks”.
The basic syntax for scheduling a task is as follows:
schtasks /create /sc <ScheduleType> /mo <Modifier> /tn <TaskName> /tr <TaskRun>

1. <ScheduleType> can take values like minute, hourly, daily, weekly.
2. <Modifier> can take numerical values to determine the frequency of the task.
3. <TaskName> is simply a string that specifies the name of the task.
4. <TaskRun> is the actual command line code to run repeatedly.

So an R script task will often look like that (this code should go in the command line of course):
schtasks /create /sc minute /mo 30 /tn "My First R Task" /tr "Rscript path\to\the\script.R"
schtasks /create /sc daily /mo 1 /tn "My Second R Task" /tr "Rscript path\to\the\script_2.R"

To delete a task, use the following:
schtasks /delete /tn "My First R Script"

For more advanced scheduling options, check the full documentation here.

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)