在R文件中设置使用spin()

时间:2017-03-29 15:39:33

标签: r knitr r-markdown

传统上我在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")
```

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

1 个答案:

答案 0 :(得分:4)

您可以在.R文件中包含Roxygen评论。例如,

#' ## 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