This semester I’m the TA for a course on statistical graphics. The homework is done in R and submissions are required to be R Markdown (.Rmd) files. This was done so that all of the code and output are neatly presented and if the .Rmd file compiles it’s a sign that the students’ R code runs.
However, this presented a unique challenge as I wound up with too many R Markdown files to comfortably compile.
My solution (which should be OS independent) is to create a new directory with just the .Rmd files in it:
I then run R (just type <R> into your terminal emulator). A quick list.files() verifies that we’re in the right working directory:
Now, we have a list of the files that we want to compile, and the function we want to apply to each of the files is knit2html() (or knit2pdf()). However, I’ve noticed that some weird dependency/global variable strangeness happens if you don’t purge the environment before compiling each document. Luckily, knit2html()/knit2pdf() has a built-in option to handle this. The code is:
sapply(list.files(), function(x) knit2html(x, envir=new.env()))