Create parametric R markdown documentation?
knitr, markdown, pandoc, r
Solution
I would use a combination of `brew` and `knitr` to achieve this. I would create a brew template called `doc.brew` which looks like this
<% for (res in names(results)) { -%>
### Results for: <%= res %>
{r} plot(results[["<%= res %>"]]$x, results[["<%= res %>"]]$y)
<% } %>
`
You can now run the following code to get your desired output
results = list(
result1 = data.frame(x=rnorm(3), y=rnorm(3)),
result2=data.frame(x=rnorm(3), y=rnorm(3))
)
brew::brew('doc.brew', 'doc.Rmd')
knit2html('doc.Rmd')
Problem
I want to iterate over a list of result-sets in my R markdown file. When I produce the output I want to include some text like headers with the name of the result set. One hacky solution I have found is to hardcode the html output directly in the documentation like this ```` ## All results ```{r loopResults, echo=FALSE, results='asis'} results = list(result1 = data.frame(x=rnorm(3), y=rnorm(3)), result2=data.frame(x=rnorm(3), y=rnorm(3))) for(res in names(results)) { cat(paste("<h3>Results for: ", res, "</h3>>")) plot(results[[res]]$x, results[[res]]$y) } ```` This doesn't seem to be the right way to do things, especially since I want to create PDF documents via pandoc at time and would have to change the hard-coded expressions. (I have currently convenience functions like h3(text, type)). Is there a better way of doing this?