10

I'm pretty new to knitr, but I've written a script that generates a report for a county. One of the first lines in the first code chunk is display_county <- "King", and it queries a database to make all sorts of nice things about King County. Now I want to create reports for every county in my state. The only line in the script that needs be changed is the definition of display_county.

I know the brew packages is set up for stuff like this, and I know there's overlap between brew and knitr, but I don't know what I should be using.

This answer using Brew and Sweave would work with minor modifications, but is there a nice knitr way to bypass brew?

Community
  • 1
  • 1
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Is there some reason you can't make a vector of the counties and then iterate over it? – Bryan Hanson Mar 08 '13 at 23:28
  • @BryanHanson Not at all, and that's just what I did using `brew` as in Ramnath's linked answer. But it *feels* like something I ought to be able to do just in `knitr`. – Gregor Thomas Mar 09 '13 at 00:27
  • Have you tried it in `knitr`? If it works in `R` via interactive or script, I don't see why you can't do it with `knitr` too. I've never used `brew` but have used `knitr` and `sweave` extensively, and I think it should be possible. – Bryan Hanson Mar 09 '13 at 00:45
  • The key is the nature of content inside your loop. If it is mainly programmatically generated content, knitr works great. But when there is text markup, brew + knitr is cleaner. In your case, you can use child templates to achieve it using just knitr. – Ramnath Mar 09 '13 at 02:24
  • See this post [here](http://stackoverflow.com/questions/14959312/create-parametric-r-markdown-documentation/14959759#14959759). It shows loop using child templates. In either case, you need to create an extra file, and `brew` + `knitr` leads to a more compact solution. – Ramnath Mar 09 '13 at 02:47
  • 3
    for this specific application, `knit_expand()` should work well; see example 075 in https://github.com/yihui/knitr-examples – Yihui Xie Mar 09 '13 at 03:59
  • I might be making a controversial point here, but my strong belief is that `brew` + `knit` provides much cleaner solutions compared to any of the other approaches, especially when templates are involved. See this [gist](https://gist.github.com/ramnathv/5569a8d6dc20f6353485) to compare with the `knit_expand` approach. I would welcome a discussion on pros and cons here. – Ramnath Mar 09 '13 at 04:50
  • You might also try `pander` like described in http://rapporter.github.com/pander/#brew-to-pandoc - please check out the `short-code-long-report.brew` example. – daroczig Mar 09 '13 at 10:42

1 Answers1

5

If I understand correctly, you are going to use the same Rnw file for each county, so only the variable display_county will be different for each county. I would first make the call to the database to get all the names of counties and store them in a vector (say... myCounties). After that, your reports can be generated with a script containing the following:

for(dc in myCounties)  {
  knit2pdf(input='county_report.Rnw', output=paste0(dc, '_county_report.pdf'))
}

To handle errors more effectively, you can also wrap the knit2pdf call on a tryCatch statement:

for(dc in myCounties)  {
  tryCatch(knit2pdf(input='county_report.Rnw', output=paste0(dc, '_county_report.pdf')))
}
JAponte
  • 1,508
  • 1
  • 13
  • 21
  • 1
    `c` is a pretty terrible name for the index of a loop since it is also a pretty important function in R. You might consider renaming the index. – Dason Mar 21 '13 at 22:02