Generating reports for different data sets using brew and knitr

May 17, 2012

I figured out a decent way to run knitr reports on different data sets easily. For my example instead of loading a new data set in each report I just use a different random number seed and print out some data. You’ll need the brew and knitr packages installed.

First you’ll need to generate your generic template report using ‘brew’ syntax. I have the following saved in a file called “template.Rnw”. Typically brew files are stored with a .brew file extension but that was causing RStudio to freak out and I didn’t get the nice knitr syntax highlighting so I went with a .Rnw extension.


<<loadingchunk, echo=FALSE>>=

## For our example the only thing I'm changing is
## the seed used
set.seed(<%= x %>)
mydata <- rnorm(5)

Blah blah blah.

Let's look at our data using a seed of <%= x %>



Notice that this is the report I want to generate for different data sets. In my case the different data sets are created with different input seeds but this could very easily be different file names directing to different data sets to load. My template assumes that the input will be stored in “x”. After running brew on this file anything between <% %> will be replaced with what is contained in the actual R object. So if I have an number stored in x then that is what will end up being the seed for the random number generator after calling brew(“template.Rnw”).

In a separate file I have the following:

# Load the necessary packages

# Function to write report
# Note that since I used "x" in my template I need to use
# "x" as my input in <- function(x, prepend = "MyFile_"){
 rnw.file <- paste0(prepend, x, ".Rnw")
 # This generates the actual rnw file for different seeds
 brew('template.Rnw', rnw.file)
 latex.file <- paste0(prepend, x, ".tex")
 texi2pdf(latex.file, clean = TRUE, quiet = TRUE)
 out.file <- paste0(prepend, x, ".pdf")

# This is the input that will change in each report
seeds <- c(42, 314159, 642)
# This is us generating the reports!
results <- sapply(seeds,

If you have both files in the working directory and run the second file you should end up with a pdf for each of the 3 seeds we used as input.

Like I mentioned you could easily replace the seeds with different data set names to do something more useful.

One Response to “Generating reports for different data sets using brew and knitr”

  1. Thanks, great post!

    I found it by searching for a great tutorial of `brew` to reference in the docs of my similar R package.

    But after reading I realized I might even advertise my package here, as the above task could be handled in even one brew-like file without any calls to e.g. `xtable` etc. and decent caching. Please my `short-code-long-report` example if you would have time:

Leave a Reply

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

You are commenting using your 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 )

Connecting to %s

%d bloggers like this: