Flow charts are actually really easy in R Markdown if you make use of the DiagrammeR R package. This package is great because it adds support for both Graphiz and MermaidJS diagram formats (I have included DiagrammeR package in the docker image).

Graphs are pretty complicated so I have linked to their documentation here and will only show a few basic examples in this guide.

Note: As of time of writing this blog there was a problem inserting charts into non html documents. To work around this I added webshot to the docker image which will screenshot any html elements and add them as a png to documents.

Inserting a basic graphiz graph in your document.

Here is an example from the DiagrammeR documentation. Pretty simple to do you just need to import the DiagrammeR library then call grViz and put the Graphviz text inside a string.

``` {r, echo = FALSE}
library(DiagrammeR)
grViz("
digraph dot {

graph [layout = dot]

node [shape = circle,
      style = filled,
      color = grey,
      label = '']

node [fillcolor = red]
a

node [fillcolor = green]
b c d

node [fillcolor = orange]

edge [color = grey]
a -> {b c d}
b -> {e f g h i j}
c -> {k l m n o p}
d -> {q r s t u v}
}")
```

Inserting a basic mermaid graph

Like the graphvix example above I have put the mermaid style markup example from the DiagrammR docs here. Again just import the DiagrammeR library and then call mermaid with a string containing mermaid notation.

```{r results="asis",eval=TRUE,echo=FALSE,message=FALSE, error=FALSE, warning=FALSE, comment = NA,fig.height=10}
library(DiagrammeR)

mermaid("
graph LR
  A-->B
  A-->C
  C-->E
  B-->D
  C-->D
  D-->F
  E-->F
")
```

Which one to use

If you are wondering at this point which one to use my advice is use Mermaid for simple diagrams because its notation is a lot easier and use Graphiz for complex graphs.

RMarkdown Series