Knitr: one plot per .tabset in for-loop
<p>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.</p>

<p>Dynamically creating the tabsets themselves is easy. Here is my .R file</p>

<pre><code>#' ---
#' title: 
#' author: 
#' date: 
#' output:
#'    html_document
#' ---

#' # {.tabset}
#+ results='asis', echo=FALSE
for(i in 1:10){
  cat('##',i,' \n')
}
</code></pre>

<p>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.
<a href="https://i.stack.imgur.com/7ORhA.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/7ORhA.jpg" alt="enter image description here"></a>
Now I add in a boxplot() call to the for-loop and expect to get the same boxplot on each tabset.</p>

<pre><code>#' ---
#' 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)
}
</code></pre>

<p>But instead, the first tab comes up empty and the second tab is this vomit of output.
<a href="https://i.stack.imgur.com/Jcn3p.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/Jcn3p.jpg" alt="enter image description here"></a></p>

<p>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.</p>

<pre><code>#' ---
#' 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')
}
</code></pre>

<p><a href="https://i.stack.imgur.com/fLTrO.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/fLTrO.jpg" alt="enter image description here"></a></p>