在 R 文件中设置使用 spin() 呈现的部分标题



传统上,我在 Rmd 文件中声明节标题,如下所示:

# header 1
```{r, echo=FALSE}
print("foo")
```
## header level 2
```{r}
print("bar")
```

因此,当它呈现为 HTML 时,它看起来像这样。我可以放一个目录。

页眉 1

foo

标头级别 2

bar

使用 spin(( 函数,我想从 R('Main.R'( 文件生成一个 Rmd 文件,该文件在 knitr 中具有正确的标头。目前如果我有这个 R 文件:

#+ section_1, echo=F
# what do I put here so that "header 1" gets inserted into the spun Rmd?
print("foo")
#+ section_2
print("bar")

spin('Main.R', knit=F)纺纱会产生这样的结果:

```{r section_1, echo=F}
# what do I put here so that "header 1" gets inserted into the spun Rmd?
print("foo")
```{r section_2}
print("bar")
```

我找不到任何建议如何插入部分名称的文档?

您可以在 中包含 Roxygen 注释。R 文件。 例如

#' ## Section 1
print("foo")
#' ## Section 2
print("bar")

如果需要,可以包含完整的 YAML 标头,例如

#' ---
#' title: My document in a .R file
#' output:
#'   html_document: 
#'     toc: true
#' ---
#' 

但是您可能需要使用rmarkdown::render而不是knitr::spin

最新更新