Reputation: 3496
How can I generate a narrative functionally in RMarkdown? For instance, say I want to generate 3 headers like
##First
##Second
#Third
from a vector c('First', 'Second', 'Third')
, then knit to pdf
Upvotes: 1
Views: 45
Reputation: 2552
You can reference a pre-defined list of headers in RMarkdown as follows:
```{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]`
Upvotes: 1
Reputation: 4224
If you're trying to knit to pdf, this solution calls on LaTeX within an R code chunk to create the headers as you requested. Just add this into an .Rmd file:
---
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,"}"))
```
The output is:
And if you don't want the R code chunk to show in the pdf, just supply echo=FALSE in the code chunk options.
Upvotes: 1