Mermaid Diagrams in RMarkdown

A nice tool to have at your disposal is a method for quickly making a connected graph diagram in your markdown. It may be a workflow you are trying to show or something an org chart or whatever. One thing that I’ve recently learned to through Obsidian as I develop my second brain (I call it my brainforest) is that you can easily insert mermaid charts into markdown and have them render properly. In R, it is the same, though we have a few tricks we need to use so that it looks nice.

library(DiagrammeR)

A mermaid chart is a simple textual representation of nodes and connections denoted as a string. There is a top down graph-like flowchart.

mermaid("graph TD; 
    A[Start] --> B{Is it?}; 
    B -- Yes --> C[OK]; 
    C --> D[Rethink]; 
    D --> B; 
    B -- No --> E[End];")

The thing that I find a bit of a pain (in RMarkdown though apparently not here in blogdown) is that the box around the plot in RMarkdown is a bit too large. To correct for that, we need to add to the function call that the diagram needs to take up 100% of the space.

mermaid("graph LR; 
A(Process 1)-->B(Process 2);
B-->C(Analysis);
C-->D(Visualization);
D-->E(QA/QC);",
        width="100%",height="100%")

So that it all fits together.

Father, Husband, Brewer, Professor

Middle aged guy trying to keep it all together and figure out how to best navigate the world as it is. Technology geek, practitioner of fermentation sciences, researcher, biologist.

Related