Search code examples
rr-markdowndiagrammermermaid

DiagrammeR/mermaid flowchart in Rmarkdown file with output format PDF/LaTex


I would like to include a mermaid diagram in a PDF generated with R markdown.

According to this post, mermaid creates an HTML widget as output. Unfortunately, the answer provided there for xaringan slides does not work for PDFs generated in R markdown.

A Rmd-MWE is below. Any help is greatly appreciated!

---
title: "DiagrammeR: mermaid diagram in Rmd"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Simple mermaid diagram

```{r}
library(DiagrammeR)
mermaid("
graph LR
    A-->B
    ", height = '100%', width = '100%')
```

Solution

  • Run these two lines in your Rstudio console:

    install.packages('webshot')
    webshot::install_phantomjs()
    

    https://bookdown.org/yihui/bookdown/html-widgets.html