I am trying to generate R Markdown document with headers generated dynamically from a loop.
I used suggestions from similar SO questions: Knitr: print text from code block as R markdown, Using bold text in a loop for a Word document in Rmarkdown and wrote
---
title: "Untitled"
output:
html_document:
toc: true
toc_depth: 5
---
## R Markdown
```{r, warning=FALSE, message=FALSE, echo=FALSE}
library(ggplot2)
library(dplyr)
```
```{r fig.width=4, fig.height=2, echo=FALSE, results='asis'}
experiment_names <- paste0("Experiment ", 1:3)
for (id in 1:3){
cat(" \n")
cat("### ", experiment_names[id])
cat(" \n")
set.seed(id)
plt <-
data.frame(x = rnorm(100)) %>%
ggplot(aes(x = x)) +
geom_histogram(binwidth = 0.1)
plot(plt)
}
```
but only the 1st loop iteration header is printed correctly. How to get the two others working too?