Mermaid diagrams not rendering correctly in Rmarkdown xaringan presentations

落花浮王杯 提交于 2020-02-25 05:00:07

问题


I am attempting to make some simple flowcharts in an Rmarkdown html presentation I am rendering with xaringan. I'm drawing mermaid diagrams using the DiagrammeR package. However, although the charts display correctly in the Rstudio viewer the styling does not appear in the presentation output.

For instance

DiagrammeR::mermaid("
graph LR;
A((Orange)) --> B((Grey));

classDef orange fill:#f96;
classDef grey fill:#d3d3d3;
class A orange;
class B grey;
")

generates one orange node and one grey node as expected when run at the console. However,

---
title: "Simple Example" 
output: 
  xaringan::moon_reader
---


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

## Flow chart

```{r example, fig.align='center', fig.retina=3}
DiagrammeR::mermaid("
graph LR;
A((Orange)) --> B((Grey));

classDef orange fill:#f96;
classDef grey fill:#d3d3d3;
class A orange;
class B grey;
")
```

generates the flowchart in the default mermaid colors ignoring the styling.

Does anyone know a workaround for this? I would also be open to suggestions of other packages for drawing simple tree diagrams.


回答1:


The mermaid creates a htmlwidget as output. You should wrap it into a <iframe> section. The widgetframe package can do this for you, other htmlwidget-based apps like DT, leaflet, Dygraph can be embeded into xaringan with this method.

---
title: "Simple Example" 
output: 
  xaringan::moon_reader
---


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

## Flow chart

```{r example, fig.align='center', fig.retina=3}

suppressPackageStartupMessages(library(widgetframe))


l=DiagrammeR::mermaid("
graph LR;
A((Orange)) --> B((Grey));

classDef orange fill:#f96;
classDef grey fill:#d3d3d3;
class A orange;
class B grey;
")

widgetframe::frameWidget(l)
```


来源:https://stackoverflow.com/questions/58689080/mermaid-diagrams-not-rendering-correctly-in-rmarkdown-xaringan-presentations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!