I wrote earlier about using Rakefiles with Pandoc. Now that I’m engaged in writing a series of posts on historical history dissertations, I have another problem that Rake can solve. Each of the blog posts for the series is written in R Markdown, which lets me mix R code and writing together in a kind of literate programming or research notebook. Then the fabulous knitr package by Yihui Xie runs the code to create a combination of writing and output in a Markdown file.

Let’s assume that you have a set of R Markdown files in your project directory, and that you want to knit them without loading any .Rdata or .Rprofile files so that your research is reproducible. You can do this by with a Rakefile like this:

<span class="c1"># Find the source files and generate list of corresponding output files</span>
<span class="no">KNITR_FILES</span> <span class="o">=</span> <span class="no">FileList</span><span class="p">[</span><span class="s2">"*.rmd"</span><span class="p">]</span>
<span class="no">OUTPUT_MDS</span>  <span class="o">=</span> <span class="no">KNITR_FILES</span><span class="p">.</span><span class="nf">ext</span><span class="p">(</span><span class="s2">".md"</span><span class="p">)</span>

<span class=“n”>rule</span> <span class=“s2”>".md"</span> <span class=“o”>=></span> <span class=“s2”>".rmd"</span> <span class=“k”>do</span> <span class=“o”>|</span><span class=“n”>t</span><span class=“o”>|</span> <span class=“n”>sh</span> <span class=“sx”>%[Rscript –vanilla -e “library(knitr); knit('#{t.source}');”]</span> <span class=“k”>end</span>

<span class=“n”>desc</span> <span class=“s2”>“Run knitr on all the analysis files”</span> <span class=“n”>task</span> <span class=“ss”>:analysis</span> <span class=“o”>=></span> <span class=“no”>OUTPUT_MDS</span>

I have some additional options for this kind of project Rakefile, which you can [see here](https://github.com/lmullen/dissertations-data/blob/master/Rakefile). Mostly they run data munging scripts and define cleaning tasks.