Mermaid Diagrams in RMarkdown

A short description of the post.

Rodney Dyer https://dyerlab.org (Center for Environmental Studies)https://ces.vcu.edu
2021-03-30

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.

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.

Citation

For attribution, please cite this work as

Dyer (2021, March 30). The Dyer Laboratory: Mermaid Diagrams in RMarkdown. Retrieved from https://dyerlab.github.io/DLabWebsite/posts/2021-03-30-mermaid-diagrams-in-rmarkdown/

BibTeX citation

@misc{dyer2021mermaid,
  author = {Dyer, Rodney},
  title = {The Dyer Laboratory: Mermaid Diagrams in RMarkdown},
  url = {https://dyerlab.github.io/DLabWebsite/posts/2021-03-30-mermaid-diagrams-in-rmarkdown/},
  year = {2021}
}