Rmarkdown snippets


Published 2021-07-06

Render with parameters

First, write parameters in the YAML of the Rmarkdown file. E.g. if you want to parameterize the title of the rendered file, you could do this:

---
output: html_document
params: 
    set_title: "My Title!"
title: "`r params$set_title`"
---

Then, from R, run rmarkdown::render:

rmarkdown::render(
    "MyDocument.Rmd", 
    params = list(
        set_title = "New Title"
    )
)

Generate markdown text inside a loop

Set code block results to "asis", and use cat in the loop.

```{r echo = FALSE, results = "asis"}
sections <- c('a', 'b', 'c', 'd')

for (new_section in sections) {
    cat(paste0('\n\n ## New section: ': new_section))
}
```

Render pretty tables

sample_table %>%
    flextable() %>% 
    autofit() %>% 
    flextable_to_rmd()

Add a line break when rendering to PDF

\newpage

Pages that link to this page