Skip to content

Instantly share code, notes, and snippets.

@cderv
Last active January 27, 2019 16:03
Show Gist options
  • Save cderv/b2afa294c9f41a8ece4df8412f47eaab to your computer and use it in GitHub Desktop.
Save cderv/b2afa294c9f41a8ece4df8412f47eaab to your computer and use it in GitHub Desktop.
Examples for Rmarkdown cookbook
---
title: "Programmatically create a document"
date: "`r Sys.Date()`"
output:
pagedown::html_paged:
toc: true
# change to true for a self-contained document, but it'll be a litte slower for Pandoc to render
self_contained: false
---
```{r, include = FALSE}
template_file <- tempfile(pattern = "template", fileext = ".Rmd")
```
```{cat, engine.opts = list(file = template_file)}
<!-- you can write like in a rmarkdown file here -->
# Child document for row {{row}}
This is an R Markdown child document.
Voici la valeur des variables :
- `Sepal.Length` : {{Sepal.Length}}
- `Sepal.Width` : {{Sepal.Width}}
- `Petal.Length` : {{Petal.Length}}
- `Petal.Width` : {{Petal.Width}}
- `Species` : {{Species}}
```
# About
This is a document composed of chapter from rows of a data frame
```{r, include = FALSE}
library(purrr)
chunks <- iris %>%
# add other variable in the table like row
tibble::rownames_to_column("row") %>%
purrr::pmap(function(...) {
knitr::knit_expand(template_file, ...)
})
res <- knitr::knit_child(text = unlist(chunks))
```
`r res`
```{r clean, include = FALSE}
unlink(template_file)
```
---
title: "Programmatically create a document"
date: "`r Sys.Date()`"
output:
pagedown::html_paged:
# change to true for a self-contained document, but it'll be a litte slower for Pandoc to render
toc: true
self_contained: false
html_document: default
---
```{r, include = FALSE}
library(purrr)
chunks <- iris %>%
# add other variable in the table like row
tibble::rownames_to_column("row") %>%
glue::glue_data("
# Child document for row {row}
This is an R Markdown child document.
Voici la valeur des variables :
- `Sepal.Length` : {Sepal.Length}
- `Sepal.Width` : {Sepal.Width}
- `Petal.Length` : {Petal.Length}
- `Petal.Width` : {Petal.Width}
- `Species` : {Species}
") %>%
glue::glue_collapse(sep = "\n")
```
# About
This is a document composed of chapter from rows of a data frame
`r chunks`
@cderv
Copy link
Author

cderv commented Jan 27, 2019

yes you're right. I think you can't without any escaping. It is more like

you can write like in a markdown file.

I'll work on a clever example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment