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
    set_title: "My Title!"
title: "`r params$set_title`"

Then, from R, run rmarkdown::render:

    params = list(
        set_title = "New Title"

Use R objects in Rmarkdown parameters

Start the parameter string with !r, like so:

title: "Untitled"
output: html_document
  n: 100
  test: !r c(mtcars, cars)
```{r test}

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() %>% 

Add a line break when rendering to PDF


Pages that link to this page