交互式 headers R 降价

Interactive headers R markdown

我想将交互式 header 添加到降价文档中。

假设我们有以下降价文档:

---
title: "Non-interactive header"
output: 
   html_document:
      toc: true
---

## 1 plot
```{r, echo=FALSE}
plot(1, 1)
```

## 2 plot
```{r, echo=FALSE}
plot(1, 1)
```

这将创建一个包含 2 个 header 的 html 文档,分别称为“1 plot”和“2 plot”。每个 header 后面都有情节。但是,我想基于变量交互式地创建 headers。根据此处找到的建议 (http://biochemistri.es/modular-workbook),我想出了这个:

---
title: "Interactive header"
output: 
   html_document:
      toc: true
---

```{r, echo=FALSE, results = 'asis'}
for (i in paste(1:10, "plot")) {
  cat(paste("##", i), sep = "\n")
}
```

乍一看效果很好。 html 文档由 10 个 header 组成,称为 1plot,2 plot 等等。不幸的是,一旦我将代码添加到 headers,该图仅在第二个 header 之后显示,随后的 headers 不再显示为 header。

---
title: "Interactive header"
output: 
   html_document:
      toc: true
---

```{r, echo=FALSE, results = 'asis'}
for (i in paste(1:10, "plot")) {
  cat(paste("##", i), sep = "\n")
  plot(1, 1)
}
```

所以问题是:如何将 r-code 添加到每个交互式 header?

我发现可以在情节后添加两个换行符来实现。

---
title: "Interactive header"
output: 
   html_document:
      toc: true
---

```{r, echo=FALSE, results = 'asis'}
for (i in 1:10) {
  cat(paste("## Plot", i), sep = "\n")
  plot(i, 1)
  cat("\n\n")
}
```