Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export mermaid chart from DiagrammeR

I'm attempting to export a Gantt chart from mermaid to a file through R. I'd be happy with any file format, but SVG or PNG would be preferable. I'm trying to automate this, so simply pressing export through the GUI is not an option.

Here's my code:

library(DiagrammeR)
graph <- mermaid("
    gantt
    dateFormat  HH:mm:ss.SSS
    title Sample Test Gantt

    section A
    thing1          :   15:58:51.556,   16:05:23.494

    section B
    thing2          :   16:02:00.391,   16:20:46.533

    section C
    thing3          :   16:18:57.352,   16:23:10.700
    thing4          :   16:24:11.705,   16:30:30.432
    ")
graph

And the graph it generates: A sample Gantt chart

like image 799
LemenDrop Avatar asked Mar 05 '23 16:03

LemenDrop


2 Answers

This is a reported issues with the DiagrammeR package, so you may want to keep an eye on this page for future updates: https://github.com/rich-iannone/DiagrammeR/issues/66

There are two ways this could be done as a workaround:

Using Webshot

An alternative way of saving the file is to use the webshot package. This uses the external dependency phantomjs to convert the HTML widget to an image. It can be setup as follows:

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

Using your above example:

library(DiagrammeR)
library(magrittr)


gannt %>%
  htmltools::html_print() %>%
  webshot::webshot(file = "gannt.pdf")

This will save the plot as a PDF, but you can create images by changing the filename i.e. gannt.png.

Using the plotly package

The plotly package has a useful function for exporting HTML widgets:

plotly::export(gannt, file = "mermaid.png")
like image 112
Michael Harper Avatar answered Mar 18 '23 08:03

Michael Harper


From what I know about mermaid it is not possible yet to export to svg or other formats. But it is possible to dump many mermaid objects to an HTML via Rmd:

---
title: "Untitled"
author: "Me"
date: "August 1, 2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. 

```{r echo=FALSE, warning=FALSE, message=FALSE}
library(DiagrammeR) 
   graph <- mermaid("
   gantt
   dateFormat  HH:mm:ss.SSS
   title Sample Test Gantt

   section A
   thing1          :   15:58:51.556,   16:05:23.494

   section B
   thing2          :   16:02:00.391,   16:20:46.533

   section C
   thing3          :   16:18:57.352,   16:23:10.700
   thing4          :   16:24:11.705,   16:30:30.432
   ")
graph
graph
graph
```

It produces an HTML file with all the graphs in it. Not an optimal solution, but better than trying to produce lots of charts manually.

like image 21
mysteRious Avatar answered Mar 18 '23 08:03

mysteRious