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

Viewed 2125

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%')
```
2 Answers

Here is a workaround. Replace the code in your last chunk with this:

library(DiagrammeR)
library(networkD3)
library(webshot)

g  <- mermaid("
graph LR
    A-->B
    ", height = '100%', width = '100%')

saveNetwork(g, "g.html")


webshot("g.html", "g.png", vheight = 50)
Related