I'm trying to use knitr to generate a report that performs the same set of analyses on different subsets of a data set. The project contains two Rmd files: the first file is a master document that sets up the workspace and the document, the second file only contains chunks that perform the analyses and generates associated figures.
What I would like to do is knit the master file, which would then call the second file for each data subset and include the results in a single document. Below is a simple example.
Master document:
# My report ```{r} library(iterators) data(mtcars) ``` ```{r create-iterator} cyl.i <- iter(unique(mtcars$cyl)) ``` ## Generate report for each level of cylinder variable ```{r cyl4-report, child='analysis-template.Rmd'} ``` ```{r cyl6-report, child='analysis-template.Rmd'} ``` ```{r cyl8-report, child='analysis-template.Rmd'} ```
analysis-template.Rmd:
```{r, results='asis'} cur.cyl <- nextElem(cyl.i) cat("###", cur.cyl) ``` ```{r mpg-histogram} hist(mtcars$mpg[mtcars$cyl == cur.cyl], main = paste(cur.cyl, "cylinders")) ``` ```{r weight-histogam} hist(mtcars$wt[mtcars$cyl == cur.cyl], main = paste(cur.cyl, "cylinders")) ```
The problem is knitr does not allow for non-unique chunk labels, so knitting fails when analysis-template.Rmd
is called the second time. This problem could be avoided by leaving the chunks unnamed since unique labels would then be automatically generated. This isn't ideal, however, because I'd like to use the chunk labels to create informative filenames for the exported plots.
A potential solution would be using a simple function that appends the current cylinder to the chunk label:
```r{paste('cur-label', cyl, sep = "-")} ```
But it doesn't appear that knitr will evaluate an expression in the chunk label position.
I also tried using a custom chunk hook that modified the current chunk's label:
knit_hooks$set(cyl.suffix = function(before, options, envir) { if (before) options$label <- "new-label" })
But changing the chunk label didn't affect the filenames for generated plots, so I didn't think knitr was utilizing the new label.
Any ideas on how to change chunk labels so the same child document can be called multiple times? Or perhaps an alternative strategy to accomplish this?
Code chunks in an R Markdown document contain your R code. All code chunks start and end with ``` – three backticks or graves. On your keyboard, the backticks can be found on the same key as the tilde (~).
To set global options that apply to every chunk in your file, call knitr::opts_chunk$set in a code chunk. Knitr will treat each option that you pass to knitr::opts_chunk$set as a global default that can be overwritten in individual chunk headers.
You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).
RMarkdown is an extension to markdown which includes the ability to embed code chunks and several other extensions useful for writing technical reports. The rmarkdown package extends the knitr package to, in one step, allow conversion between an RMarkdown file (.Rmd) into PDF, HTML, word document, amongst others.
For anyone else who comes across this post, I wanted to point out that @Yihui has provided a formal solution to this question in knitr 1.0 with the introduction of the knit_expand()
function. It works great and has really simplified my workflow.
For example, the following will process the template script below for every level of mtcars$cyl
, each time replacing all instances of {{ncyl}}
(in the template) with its current value:
# My report ```{r} data(mtcars) cyl.levels <- unique(mtcars$cyl) ``` ## Generate report for each level of cylinder variable ```{r, include=FALSE} src <- lapply(cyl.levels, function(ncyl) knit_expand(file = "template.Rmd")) ``` `r knit(text = unlist(src))`
Template:
```{r, results='asis'} cat("### {{ncyl}} cylinders") ``` ```{r mpg-histogram-{{ncyl}}cyl} hist(mtcars$mpg[mtcars$cyl == {{ncyl}}], main = paste({{ncyl}}, "cylinders")) ``` ```{r weight-histogam-{{ncyl}}cyl} hist(mtcars$wt[mtcars$cyl == {{ncyl}}], main = paste({{ncyl}}, "cylinders")) ```
If you make all chunks in your ** nameless, i.e. ```{r}
it works. This, of course, is not very elegant, but there are two issues preventing you from changing the label of the current chunk:
The fact that unnamed blocks work is that internally they get the label unnamed-chunk-
+chunk number.
Blocks cannot have duplicate names as internally knitr references them by label. A fix could be to make knitr add the chunk number to all chunks with duplicate names. Or to reference them by chunk number instead of label, but that seems to me a much bigger change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With