Scheduling Rmarkdown files on Windows – your foolproof guide

[This article was first published on HighlandR, 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.

You’ve written a great rmarkdown file, and now you want to run it at a set time every day. You’ve tried the Windows Task Scheduler, you’ve pointed it at your markdown file, and you’ve gone off to make some coffee safe in the knowledge that your outputs will be waiting for you when you get back to your desk…

…Only they aren’t, because something’s gone wrong, and you don’t know what it is.

Does this sound familiar?

I won’t be the only one who’s struggled with this – scheduling rmarkdown files is harder than a regular R script.

After a lot of trial and error, and finding little snippets from blogs, videos and forums, here is the inside scoop on what can go wrong, how to fix it, and how you too can schedule your rmarkdown scripts to run automatically with Windows Task Scheduler.

There are three solutions detailed here:

  • create an R script to render your markdown file
  • create a batch file within the same directory as your markdown file
  • create a batch file that runs from your desktop or elsewhere

What can wrong?

Here are some of the issues I came across:

  • Windows Task Scheduler (hereafter WTS) doesn’t know where your R install is
  • WTS doesn’t know where your Pandoc install is
  • Your packages aren’t necessarily where they need to be
  • You’ve set up the task incorrectly

Solution 1 – render markdown file from another R file in the same directory

First of all, you need to know where your Pandoc install is located. I recommend opening notepad or other text editor, and start logging some details

Sys.getenv("RSTUDIO_PANDOC")

## [1] "C:/Program Files/RStudio/bin/pandoc"

Once you’ve got this, you can set up a regular R script in the same directory as your rmarkdown file.

Let’s assume it is called ‘render.R’, with the following code:

library(rmarkdown)
Sys.setenv(RSTUDIO_PANDOC = 'C:/Program Files/RStudio/bin/pandoc')
rmarkdown::render(
  input = "Z:/Some/Folder/scheduled_report.Rmd",
  output_file = "Z:/Some/Folder/scheduled_report.html",
  output_dir = "Z:/Some/Folder"
)

I’m going belt and braces here – I said ‘foolproof’ remember?

Before you do anything else – once you set up your regular R script as above, source it within R itself, and make sure it renders your Rmarkdown correctly. If it does, (and it should, assuming you have the rmarkdown package installed, then you are good to move onto the next bit.

Potential spanner in works klaxon

We’ve dealt with one of the 4 big gotchas (location of Pandoc).
Here is a less obvious one – on Windows, you might find your R install, and your ‘win-library’ installs (packages you install in addition to base), are in different locations, and this really messes things up.

Why? If the necessary packages aren’t where you tell WTS your R install is, then your script will fail, possibly silently, but definitely agonisingly.

For example, my R install is here: C:\Program Files\R\R-4.1.0\library (my install is not bang up to date)

But for some reason, my default location for packages that I install on top of the base install, is here:

C:\Users\me\OneDrive\Documents\R\Win-Library\4.1

The point is, WTS will be looking in C:\Program Files\R\R-4.1.0\library , but the packages I need for my .Rmd file to run might not be there, resulting in frustration, desolation, and possibly being absolutely raging, as we say in Scotland.

So, to deal with gotcha number 2 – make sure the packages you need are installed in C:\Program Files\R\R-4.1.0\library (obviously adapt this for your current version and default R installation location)

To the Task Scheduler

If you’re happy at this point, we’re almost good to go. Search for Task Scheduler in the wee magnifying glass bottom left on Windows, and you’ll find it, then open it up), and choose Create Task from the menu on the right.

You can work through the first two tabs ( General and Triggers) but I like to go straight to the Actions tab.

Here’s what it will look like at first:

blank

We just need to fill in the blanks:

Here are the values I supplied

Program/script – "C:\Program Files\R\R-4.1.0\bin\Rscript.exe" You’ll find your path by hitting the ‘Browse’ button, and navigating to your R install, typically in Program Files.

Because ‘Program Files’ has a space, we need to ensure that we wrap this full path in quotes. The best way to do this is to navigate to the bin folder, hold down the SHIFT key, and then right click the Rscript.exe file, and choose Copy as path option, Then paste it right in to the Program/script section

program

We then need to point it to our R script.

The ‘Add arguments’ section is not optional. That’s where we put the name of our R file.

arguments

The final, allegedly optional argument, is the ‘Start in’ section. We are absolutely filling that bit in – with the path to the folder that contains our R script and Rmd files.

To recap – we have this file, called render.R, that calls scheduled_report.rmd

library(rmarkdown)
Sys.setenv(RSTUDIO_PANDOC = 'C:/Program Files/RStudio/bin/pandoc')
rmarkdown::render(
  input = "Z:/Some/Folder/scheduled_report.Rmd",
  output_file = "Z:/Some/Folder/scheduled_report.html",
  output_dir = "Z:/Some/Folder"
)

In WTS Actions tab, add the following :
Program/script ="C:\Program Files\R\R-4.1.0\bin\Rscript.exe"
Add arguments = render.R
Start in = Z:/Some/Folder

That’s it. The General tab is easy enough, just fill in the blanks ( give the task a name and description) and on the triggers tab you can set a date and time to run the script. I recommend running it manually – if it finishes really quickly, or you don’t see the command window opening up and some gloriously familiar rendering code as your R script works, then something has gone wrong.

Here is how mine looks:

complete

It seems that either \ or / work equally well in the ‘Start in’ section.

You should be able to run this manually, and it should work. You can now schedule it for a few minutes ahead, and double check that it’s still fine. If so, set it to the schedule you want. You’re done.

Solution 2 – create and schedule a .bat file within the same directory as your markdown file

Maybe rendering rmarkdown from R seems too much. You can create a batch file (create a blank text file, and save it as *.bat) in the same directory. Then add the necessary info (path to Rscript, Pandoc location, name of source file and output file).

Based on the above info, here is what you need in the batch file:

"C:\Program Files\R\R-4.1.0\bin\Rscript.exe" -e "library(rmarkdown);Sys.setenv(RSTUDIO_PANDOC='C:/Program Files/RStudio/bin/pandoc'); rmarkdown::render('scheduled_report.Rmd',output_file='scheduled_report.html')

Adjust according to your R install location and file names.

Note the mix of \ and / for the relevant Windows section and then the R commands/ arguments

Solution 3 – batch file running from the desktop or elsewhere

If you don’t want to navigate to your bactch file for editing, or for convenient ad-hoc running, you might want your batch file on the desktop or alternate location.

The only difference is you need to provide full paths to the rmd and output files:

"C:\Program Files\R\R-4.1.0\bin\Rscript.exe" -e "library(rmarkdown);Sys.setenv(RSTUDIO_PANDOC='C:/Program Files/RStudio/bin/pandoc'); rmarkdown::render('Z:\SOME\FOLDER\scheduled_report.Rmd',output_file='Z:\SOME\FOLDER\scheduled_report.html')"

You can enter @echo off at the start of these batch files if you don’t want to see any output from the command window. Personally, I like to see it appear, if only for peace of mind.

You can also insert a pause at the end – you will be prompted to press any key to continue. This is handy when setting things up, as at least you can see where any errors occur.

Here’s how mine looked when running from the same directory:

bat

This time, the program is the batch file, not Rscript.exe, and we provide the full path in the ‘Start in’ section.

Wrap up

I’m pretty confident you can get this to run.

Here’s what to check:

  1. Your R installation location
  2. Your additional downloaded packages are in the same location as your R install (copy over the necessary ones if not)
  3. Your Pandoc location
  4. You are using \ and / correctly for both navigating Windows and providing arguments to R

If all the above is in order, you can render your rmarkdown file by calling a small, regular R script. Or you can create a batch file, either in the same project directory, or elsewhere. The only difference is that you will need to ensure you fully qualify the filepaths if you choose the latter option.

Happy scheduling!

bob

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

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)