Scheduling R Scripts on Windows
December 2, 2017
When I first started hearing and learning about R, it was presented to me as more of a “statistical language.” Admittedly, my statistical knowledge is still growing and I questioned whether learning R would be useful for me. However, as I dove in, I quickly learned that R is a powerful tool for many tasks other than statistical modeling. In fact, I would say my primary use for R is data wrangling.
One of the primary functions of the Data and Operations team at Fetch is gathering and managing data for our data platform, FetchMe. It’s no secret that the advertising world is driven by tons of data which of course means that advertising campaigns have tons of data sources, all of which have their own quirks. Some of my favorite R Packages are those that can connect to the web and help me pull data from various APIs to then clean data in a more automated, efficient manner. While these packages help eliminate the time it would normally take to navigate and download data through a UI, they still require that I run the scripts in RStudio.
Recently, I stumbled upon an R package to help schedule scripts to run automatically. Unfortunately, for reasons unknown, I suspect security settings placed on my machine, I could not get these packages to work like I need. Alias, several Google searches later, I found a YouTube video and a few other articles and that helped me. Below, I’ve put together a step by step list that I follow to help set up automated R Scripts on Windows (Sorry Mac users! Maybe the package will work for you!).
Step 1 - Add R to Environment Variables
Run this code in RStudio to find where R is located on your computer:
gsub("library", "bin", .libPaths())
Copy this path. It should look something like this:
Navigate to the Environment Variables by:
- Right clicking on This PC in a File Explorer Window > Properties
- Choose Advanced System Settings
- Choose “Environment Variables”
- Highlight Path and Click Edit > New > Paste the Path from R
- Save/Apply Settings
Step 2 – Confirm Path was added
Open up Command Prompt by typing in “CMD” into Search Windows. Next, type “r” into the Command Prompt. If it worked you’ll see the same welcome message that you see in RStudio.
If you receive an error, double check that you use added the correct path from R. You may need to move the path “up” in the list of Environment Variables. If that doesn’t work, just Google it :)
Step 3 – Create .bat file
This file will be used in TaskSheduler. You’ll need a text editor like Notepad or SublimeText. The file will be short and will look like this:
@echo off R CMD BATCH C:\Users\nbautista\Desktop\MyScheduleR\ScheduledReport.R
The Path above is where the R Script is located on your computer. I’ve found that this works best if the R script lives locally. You can still save a file to a shared drive or dropbox but I believe Task Scheduler can only run batch files locally. Once you’ve added the path, save the file as .bat and take note of where you save this file.
Step 4 – Schedule the Task
Search Windows for “Task Scheduler.” Next, choose Create Basic Task and name the task. Choose how often you’d like the task to run and when you’d like to Start the Task. Select “Start a Program.” Browse to where you have saved the .bat file. Finally, confirm the information you’ve just inputted. Sit back and enjoy :)
Tips, Tricks, and Notes
Your computer will need to be “awake” for this to work correctly. I use Caffeine to help keep my computer from sleeping. If you restart your computer, be sure to activate Caffeine again!
In troubleshooting with others, it seems that this works best with files and folders that don’t have spaces.
I recommend creating a separate folder for .bat files and R scripts. After doing for this several R scripts, a single folder can get messy between R, .rout, and .bat files.
If you don’t receive the output you are looking for, there should be an .Rout file that tells you what was run and if the Script failed at any point.
That should be it! Please let me know if you have any questions or run into any trouble!