LemenDrop
LemenDrop

Reputation: 160

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

Upvotes: 5

Views: 4629

Answers (3)

LemenDrop
LemenDrop

Reputation: 160

Marked mysteRious' answer as correct, as it really helped. For anyone in the future that runs into the same issue, here's the full solution I used:

Export.Rmd

---
title: "TestingExport"
author: "me"
date: "August 2, 2018"
output: html_document
---

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

```{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
```

Then use the following command to turn this into HTML: Rscript -e "rmarkdown::render('...\\Export.Rmd')"

Upvotes: 1

Michael Harper
Michael Harper

Reputation: 15369

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")

Upvotes: 5

mysteRious
mysteRious

Reputation: 4294

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.

Upvotes: 2

Related Questions