library(rtemis.draw)
Attaching package: 'rtemis.draw'
The following object is masked from 'package:graphics':
Axis
Attaching package: 'rtemis.draw'
The following object is masked from 'package:graphics':
Axis
draw_mermaid() renders Mermaid diagram text with Pele, a JavaScript library that draws Mermaid diagrams as SVG themed by CSS variables. The diagram appears in the RStudio or VS Code Viewer pane, in Quarto and R Markdown documents, and in Shiny, and filename writes it to an SVG file from the same call.
Diagrams render at their natural size, so labels keep their font size however long or wide the diagram is. In the Viewer pane the widget fills the pane and scrolls. In a document, the widget takes the diagram’s height and scrolls horizontally when the diagram is wider than the page.
Uncolored elements use neutral grays that follow the page’s light or dark mode. Categorical entities, such as pie slices, mindmap branches, and Gantt tasks, use the theme palette or palette.
Write the source as one string, or as a character vector with one element per line. A train/test split:
title writes the title into the source’s frontmatter, replacing a title the source states.
node_color highlights selected nodes, named by node id. Each gets a border in its color and a fill of the same color at fill_alpha opacity (0.1 by default), so its label keeps the theme’s text color in light and dark mode. Here the training, validation, and test sets take the colors rtemis uses for those splits in model plots, and a subgraph groups the steps of hyperparameter tuning by resampling of the training set:
split_colors <- c(
train = rtemis_colors[["teal"]],
inner_train = rtemis_colors[["teal"]],
val = rtemis_colors[["purple"]],
test = rtemis_colors[["magenta"]]
)
draw_mermaid(c(
"flowchart TB",
" data[(Data)] --> split[Split]",
" split --> train[(Training set)]",
" split --> test[(Test set)]",
" subgraph tuning[Resampling: tuning]",
" direction TB",
" inner_split[Split j = 1, ..., m] --> inner_train[(Inner training set j)]",
" inner_split --> val[(Validation set j)]",
" inner_train --> inner_fit[Train one model per hyperparameter set]",
" inner_fit --> inner_eval[Evaluate on validation set j]",
" val --> inner_eval",
" end",
" train --> inner_split",
" inner_eval -->|validation metrics| select[Select hyperparameters]",
" select -->|selected hyperparameters| fit[Train on training set]",
" train --> fit",
" fit -->|trained model| eval[Evaluate on test set]",
" test --> eval"
), node_color = split_colors, title = "Train/test split with tuning")node_color applies to the nodes of flowchart, state, class, entity relationship, block, architecture, and kanban diagrams. A node styled in the source with style or classDef keeps that style.
Subgraphs nest. In nested resampling, the inner loop tunes hyperparameters on each outer training set, and the outer loop estimates generalization performance from the outer test sets:
draw_mermaid(c(
"flowchart TB",
" data[(Data)] --> split[Outer split i = 1, ..., k]",
" subgraph outer[Outer resampling: evaluation]",
" direction TB",
" split --> train[(Outer training set i)]",
" split --> test[(Outer test set i)]",
" subgraph tuning[Inner resampling: tuning]",
" direction TB",
" inner_split[Inner split j = 1, ..., m] --> inner_train[(Inner training set j)]",
" inner_split --> val[(Validation set j)]",
" inner_train --> inner_fit[Train one model per hyperparameter set]",
" inner_fit --> inner_eval[Evaluate on validation set j]",
" val --> inner_eval",
" end",
" train --> inner_split",
" inner_eval -->|validation metrics| select[Select hyperparameters]",
" select -->|selected hyperparameters| fit[Train on outer training set i]",
" train --> fit",
" fit -->|trained model i| eval[Evaluate on outer test set i]",
" test --> eval",
" end",
" eval -->|test metrics, all i| summary[Mean and SD of test metrics]"
), node_color = split_colors, title = "Nested resampling")A top-to-bottom flowchart describes a study’s participant flow. Long diagrams scroll rather than shrink.
draw_mermaid(c(
"flowchart TD",
" assessed[Assessed for eligibility, n = 420] --> excluded[Excluded, n = 120]",
" assessed --> randomized[Randomized, n = 300]",
" randomized --> treat[Treatment, n = 150]",
" randomized --> control[Control, n = 150]",
" treat --> treat_fu[Followed up, n = 141]",
" control --> control_fu[Followed up, n = 143]",
" treat_fu --> analysis[Analyzed, n = 284]",
" control_fu --> analysis"
))Every diagram type built into Mermaid 12.1.0 is available. A sequence diagram:
A Gantt chart; crit tasks use the critical color:
A mindmap and a pie chart take their branch and slice colors from the palette:
Pele implements Mermaid’s syntax with its own layout engine. The compatibility page lists every supported diagram type and the differences from Mermaid, and the examples page shows each type.
| Supported | Every diagram type built into Mermaid 12.1.0: flowchart, sequence, class, state, entity relationship, Gantt, git graph, pie, mindmap, kanban, timeline, user journey, quadrant chart, XY chart, requirement, Sankey, architecture, C4, block, packet, radar, treemap, Venn, and others |
| Not supported | Mermaid themes and themeVariables, ZenUML and other external plugins, HTML labels, JavaScript callbacks in click statements |
| Different | Node positions, spacing, and edge paths; a flowchart subgraph keeps its own direction when it links to nodes outside it; configuration support varies by diagram type |
| Limits | 50,000 source characters; 5,000 edges in a flowchart |
Pele uses at most eight categorical colors and repeats them from the ninth category. Colors set in the source with style or classDef override the theme and stay the same in light and dark mode; set color together with fill so labels remain legible in both.
font_size sets the label size in pixels. An explicit height fixes the widget height, and the diagram scrolls within it:
fit = TRUE shrinks the diagram to the widget width instead. Pinch or Ctrl/Cmd-scroll zooms, and dragging pans a zoomed diagram.
auto_direction = TRUE lets Pele draw left-to-right flowcharts top-to-bottom in widgets narrower than 640 pixels.
setup_MermaidConfig() describes a diagram’s appearance as a configuration with a published JSON Schema. The Mermaid source is the bound data: pass it to draw() with data, or name a .mmd file with dat_path.
<rtemis.draw::MermaidConfig>
@ type : chr "mermaid"
@ dat_path : NULL
@ title : chr "Cross-validation"
@ origin : Named chr [1:9] "default" "user" "default" "default" "default" "user" ...
.. - attr(*, "names")= chr [1:9] "dat_path" "title" "palette" "node_color" ...
@ writer : NULL
@ palette : NULL
@ node_color : NULL
@ fill_alpha : num 0.1
@ font_size : num 15
@ padding : NULL
@ auto_direction: logi FALSE
@ fit : logi FALSE
mmd <- file.path(tempdir(), "cv.mmd")
writeLines(c(
"flowchart LR",
" data[(Data)] --> folds[Split into k folds]",
" folds --> fit[Train on k - 1 folds]",
" fit --> eval[Evaluate on held-out fold]",
" eval --> summary[Summarize k estimates]"
), mmd)
draw(setup_MermaidConfig(dat_path = mmd, title = "Cross-validation"))compile() returns the PeleOption render specification that draw() renders:
filename writes the diagram as SVG at its natural size, with the theme’s colors written as literal values. Export requires Node.js 18 or later and uses the light theme unless theme is set.