Grading R Markdown Files

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:

Two .Rmd files to be compiled, both in a fresh directory.

I then run R (just type <R> into your terminal emulator). A quick list.files() verifies that we’re in the right working directory:

Running R and calling list.files() to verify that the .Rmd files we want to compile are in our current working directory.

Running R and calling list.files() to verify that the .Rmd files we want to compile are in our current 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()))
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s