Hi, I'm trying to create multiple cards using the business_cards template in pagedown (thank you for making this available!) and am having trouble understanding the multiple colon and bracket notation. E.g. in the example below, there are multiple colons before the code as well as the relevant variables (e.g. name) in the square brackets. Doesn't look like R markddown. Can you please direct me to appropriate documentation? I am trying to programmatically assign name, etc. based on a data frame I will read it. Thank you!!
::::: {.wrapper data-repeat="12"}
[Jane Doe]{slot="name"}
[Miss Nobody]{slot="title"}
[+1 123-456-7890]{slot="phone"}
[jane.[email protected]]{slot="email"}
[www.example.com]{slot="url"}
These notations are Pandocs's fenced divs and bracketed spans.
Here is the relevant section in Pandoc's documentation: https://pandoc.org/MANUAL#divs-and-spans
If you prefer, you can write the same content with HTML markup (<div></div> and <span></span>)
Ah ok, thank you! I am new to pandoc conventions. So is there a way to define name, title, etc. variables that are in the square brackets [ ] from within the r chunks, so that I can assign names and titles programmatically? instead of listing one by one. If this is not the right place for this question, please let me know where I can ask it as I'm learning this. Thank you!!
:thinking: I understand your goal, however I think I would adopt another strategy.
rmarkdown is so powerful that we can do fun things!
Let assume that we want to automate these business cards:
---
phone: "+1 844-448-1212"
email: "[email protected]"
url: www.rstudio.com
address: |
@rstudio
logo: "https://www.rstudio.com/wp-content/uploads/2016/09/RStudio-Logo-Blue-Gray-250.png"
person:
- name: Tareef Kawaf
title: President of RStudio, Inc.
repeat: 12
- name: Yihui Xie
title: Responsible for rmarkdown
email: [email protected]
url: https://yihui.name
repeat: 12
googlefonts: "Righteous"
paperwidth: 8.5in
paperheight: 11in
cols: 4
rows: 3
output: pagedown::business_card
---
The first trick that we can use is the following. With Pandoc, you can define YAML variables anywhere in your document (https://pandoc.org/MANUAL#extension-yaml_metadata_block).
_Be careful, with rmarkdown you must have a top block defining the output format._
So, this document will be rendered exactly as the previous one:
---
phone: "+1 844-448-1212"
email: "[email protected]"
url: www.rstudio.com
address: |
@rstudio
logo: "https://www.rstudio.com/wp-content/uploads/2016/09/RStudio-Logo-Blue-Gray-250.png"
googlefonts: "Righteous"
paperwidth: 8.5in
paperheight: 11in
cols: 4
rows: 3
output: pagedown::business_card
---
---
person:
- name: Tareef Kawaf
title: President of RStudio, Inc.
repeat: 12
- name: Yihui Xie
title: Responsible for rmarkdown
email: [email protected]
url: https://yihui.name
repeat: 12
---
Now, assume that we want to retrieve and prepare the data inside the R Markdown file. We could do something like that:
---
phone: "+1 844-448-1212"
email: "[email protected]"
url: www.rstudio.com
address: |
@rstudio
logo: "https://www.rstudio.com/wp-content/uploads/2016/09/RStudio-Logo-Blue-Gray-250.png"
googlefonts: "Righteous"
paperwidth: 8.5in
paperheight: 11in
cols: 4
rows: 3
output: pagedown::business_card
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r data-preparation}
person <- list(
list(
name = "Tareef Kawaf",
title = "President of RStudio, Inc.",
`repeat` = 12L
),
list(
name = "Yihui Xie",
title = "Responsible for rmarkdown",
email = "[email protected]",
url = "https://yihui.name",
`repeat` = 12L
)
)
```
---
person:
- name: `r person[[1]]$name`
title: `r person[[1]]$title`
repeat: `r person[[1]][["repeat"]]`
- name: `r person[[2]]$name`
title: `r person[[2]]$title`
email: `r person[[2]][["email"]]`
url: `r person[[2]]$url`
repeat: `r person[[2]][["repeat"]]`
---
This Rmd file leads to the same result. But this is not convenient to use.
The last step for automation is to generate the YAML block. This is possible with the yaml::as.yaml() function.
So the final step would be:
---
phone: "+1 844-448-1212"
email: "[email protected]"
url: www.rstudio.com
address: |
@rstudio
logo: "https://www.rstudio.com/wp-content/uploads/2016/09/RStudio-Logo-Blue-Gray-250.png"
googlefonts: "Righteous"
paperwidth: 8.5in
paperheight: 11in
cols: 4
rows: 3
output: pagedown::business_card
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r data-preparation}
person <- list(
list(
name = "Tareef Kawaf",
title = "President of RStudio, Inc.",
`repeat` = 12L
),
list(
name = "Yihui Xie",
title = "Responsible for rmarkdown",
email = "[email protected]",
url = "https://yihui.name",
`repeat` = 12L
)
)
```
---
```{r write-yaml, results='asis'}
cat(yaml::as.yaml(list(person = person)))
```
---
The advantage that I see in this method is that you just have to prepare your data.
Wow, this is just amazing and so helpful. Thank you for providing this level of detail! Can't wait to try it out. THANK YOU!
I don't mean to abuse your kindness, but I'm new-ish to R and Rmarkdown and have not worked with lists very much. I have trouble converting my data frame (one column has name information, another has title, etc.) into the list of lists that seem to be necessary for this to work.
When I do the following, it only produces a single card, while I need it to produce as many cards as there are rows in the data frame. Do you have any suggestions? Thank you again so very much.
roster <- read_csv(path here)
person <- list(
name = paste0(roster$student_first_name, " ", roster$student_last_name),
title = roster$title
)
Even this is quite off-topic here, here is a basic example with the starwars dataset.
The first R code chunk uses the R-base apply() function, see ?apply.
The second one uses the tidyverse purrr::pmap() function.
On this topic, @jennybc gave a great webinar, see https://www.rstudio.com/resources/webinars/thinking-inside-the-box-you-can-do-that-inside-a-data-frame/ and https://github.com/jennybc/row-oriented-workflows
You may find much more people who can help you with your data transformation steps on RStudio Community https://community.rstudio.com/ or Stack Overflow : https://stackoverflow.com/
---
logo: "https://upload.wikimedia.org/wikipedia/commons/5/5a/Star_Wars_Logo..png"
googlefonts: "Righteous"
paperwidth: 8.5in
paperheight: 11in
cols: 4
rows: 3
output: pagedown::business_card
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r r-base}
# R base
person <- apply(dplyr::starwars, 1, function(x) {
list(
name = x$name,
title = x$species,
address = x$homeworld
)
})
```
```{r tidyverse}
# tidyverse
person <- purrr::pmap(dplyr::starwars, function(name, species, homeworld,...) {
list(
name = name,
title = species,
address = homeworld
)
})
```
---
```{r write-yaml, results='asis'}
cat(yaml::as.yaml(list(person = person)))
```
---
This is perfect and worked well, thank you so much! I REALLY appreciate your help, even though this totally outside the topic. I really appreciate the link to the webinar too, this is exactly what I was looking for to get more familiar with data manipulation side of things. Thank you!!
You're welcome!
Most helpful comment
Wow, this is just amazing and so helpful. Thank you for providing this level of detail! Can't wait to try it out. THANK YOU!