Scheduling R scripts in Windows

2016-09-14 22:17 PDT

I need to schedule several R scripts on a Windows 2012 server. I've had to figure out how to do this in the past, but did not take great notes on how to set it up. So here I am hopefully putting together some useable notes on how to schedule an R script in Windows. I found a useful YouTube video on how to do this. You can find it here: https://www.youtube.com/watch?v=UDKy5_SQy2o

The basic steps are to:

  1. Add R to the scheduling user's local PATH environment variable.
    • In Windows 7 you can type "environment" into the search path and you should see a program titled "Edit environment variables for your account."
    • Create the new variable PATH for the user that will be running the scheduled task. I recommend using an account with administrative priveleges. Add the path to your preferred R installation's bin to the local PATH variable for the administrative user.

      Example:
      C:\Program Files\R\R-3.3.1\bin
  2. Write an R script to perform a task that is to be performed on a schedule. When writing the R-script keep the following in mind:
    • Make sure that any libraries used in the R-script are installed for the user under which the task will be scheduled.
    • Set the working directory within the R-script to make referencing directories easier.
  3. Create a batch script to run your R script. Example content:
          echo off
          R CMD BATCH D:\path\to\my\script.R 
  4. Use Windows Task Scheduler to run the batch script with the desired start trigger and frequency. From my experience, an administrator account should be used to author and run the scheduled task.

    If you are having trouble with your R script running when triggered, checkout the .Rout file in a text editor to observe any errors from R.