Search code examples
htmlrr-markdowndiagrammermermaid

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


Solution

  • 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.