Mar*_*ras 6 r knitr r-markdown
我正在尝试生成 R Markdown 文档,其中包含从循环动态生成的标题。
我使用了类似SO问题中的建议:Knit: print text from code block as R markdown,Using bold text in a Loop for a Word document in Rmarkdown并写道
---
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)
}
```
Run Code Online (Sandbox Code Playgroud)
但只有第一个循环迭代标题被正确打印。如何让另外两个人也工作?
我不知道为什么会这样,但尝试一行而不是三行:
cat("\n\n### ", experiment_names[id], "\n")
Run Code Online (Sandbox Code Playgroud)