如何在功能上生成标题?

时间:2017-08-30 21:11:54

标签: r r-markdown

如何在RMarkdown中以功能方式生成叙事?例如,假设我想生成3个标题,如

##First
##Second
#Third

从矢量c('First', 'Second', 'Third'),然后编织到pdf

2 个答案:

答案 0 :(得分:1)

如果您正在尝试编织为pdf,此解决方案会在R代码块中调用LaTeX,以根据您的请求创建标头。只需将其添加到.Rmd文件中:

---
title: "Untitled"
author: "Ryan Runge"
date: "8/30/2017"
output: pdf_document
---

```{r eval=TRUE, results='asis' }
headers <- c('First', 'Second', 'Third')
cat(paste("\\section{",headers,"}"))
```

输出结果为:

enter image description here

如果您不希望R代码块显示在pdf中,只需在代码块选项中提供echo = FALSE。

答案 1 :(得分:1)

您可以在RMarkdown中引用预定义的标题列表,如下所示:

```{r}
list_of_headers <- c("Title 1", "Title 2", "Title 3")
```

This is an example.

### `r list_of_headers[1]`

Stuff under the first header

### `r list_of_headers[2]`

Etc

### `r list_of_headers[3]`

Reference