Long story short, I'm trying to use knitr to dynamically create separate tabsets and render a plot within each tabset. Below is a description of my failed attempts. I would appreciate a solution, but I suspect I'm just not understanding the fundamental way in which knitr renders output. So if you could point me to resources or provide some conceptual guidance, that would also be appreciated.
Dynamically creating the tabsets themselves is easy. Here is my .R file
#' ---
#' title:
#' author:
#' date:
#' output:
#' html_document
#' ---
#' # {.tabset}
#+ results='asis', echo=FALSE
for(i in 1:10){
cat('##',i,' \n')
}
When I call rmarkdown::render() on this .R file, I get 10 empty tabsets (see below). I'm not really sure what cat does or why the \n is necessary (this is where some conceptual explanation would be appreciated), but it works.
Now I add in a boxplot() call to the for-loop and expect to get the same boxplot on each tabset.
#' ---
#' title:
#' author:
#' date:
#' output:
#' html_document
#' ---
#' # {.tabset}
#+ results='asis', echo=FALSE
for(i in 1:10){
cat('##',i,' \n')
boxplot(iris$Sepal.Length~iris$Species)
}
But instead, the first tab comes up empty and the second tab is this vomit of output.
If I add another cat() with two "\n" characters, then all the tabs appear, but only the second tab is populated with the chart. The other tabs are empty.
#' ---
#' title:
#' author:
#' date:
#' output:
#' html_document
#' ---
#' # {.tabset}
#+ results='asis', echo=FALSE
for(i in 1:10){
cat('##',i,' \n')
boxplot(iris$Sepal.Length~iris$Species)
cat(' \n \n')
}