我正在尝试生成一个带有节/标题的循环,然后在rmarkdown中加上一个数字。我知道我可以使用cat(“ ## xyz”)在我的块中生成一个新的标头,但是我观察到一些奇怪的行为。
---
title: "Untitled"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
```
Version 1: (does not work)
```{r, results='asis'}
for (i in 1:5) {
cat("\n")
cat("## This is a heading for ", i, "\n")
plot(pressure)
cat("\n")
}
```
Version 2: (does not work)
```{r, results='asis'}
for (i in 1:5) {
cat("\n")
cat("## This is a heading for ", i, "\n")
plot(pressure)
cat("\n")
plot(pressure)
cat("\n")
}
```
Version 3 (works):
```{r, results='asis'}
for (i in 1:5) {
cat("\n")
cat("## This is a heading for ", i, "\n")
plot(cars)
cat("\n")
plot(pressure)
cat("\n")
}
```
我希望输出为
标题1
图1
标题2
图2
标题3
图3
等