Renders Mermaid diagram text with Pele
(https://github.com/obsidianmd/pele) as a static SVG in an htmlwidget,
shown in the RStudio and VS Code Viewer panes, Quarto and R Markdown
documents, and Shiny. With filename, it also writes the diagram to an SVG
file.
Usage
draw_mermaid(
source,
palette = NULL,
node_color = NULL,
fill_alpha = 0.1,
font_size = 14,
padding = NULL,
auto_direction = FALSE,
fit = FALSE,
title = NULL,
theme = NULL,
width = NULL,
height = NULL,
element_id = NULL,
filename = NULL
)Arguments
- source
Character: Mermaid diagram text, as one string or one element per line.
- palette
Optional Character: Colors for categorical entities, such as pie slices and mindmap branches.
NULLuses the theme palette.- node_color
Optional Named character: Colors for selected nodes, named by node id, e.g.
c(train = rtemis_colors[["teal"]]). A node styled in the source withstyleorclassDefkeeps that style.- fill_alpha
Numeric [0, 1]: Fill opacity of the nodes in
node_color.- font_size
Numeric (0, Inf): Label font size in pixels.
- padding
Optional Numeric [0, Inf): Space around the diagram in pixels.
NULLuses Pele's default.- auto_direction
Logical: Allow Pele to draw left-to-right flowcharts top-to-bottom in widgets narrower than 640 pixels.
- fit
Logical: Shrink the diagram to the widget width, with pinch and Ctrl/Cmd-wheel zoom, instead of drawing it at its natural size.
- title
Optional Character: Diagram title, written into the source's frontmatter. It replaces a title the source states.
- theme
Optional Theme, list, or
NA: Theme override.NULLenables light/dark auto-detection.- width
Optional Character or Numeric: Widget width.
- height
Optional Character or Numeric: Widget height.
NULLfills the Viewer pane and takes the diagram's height in documents.- element_id
Optional Character: Explicit element ID.
- filename
Optional Character: Path of an SVG file to write with
save_drawing(). The SVG has the diagram's natural size.
Details
The diagram renders at its natural size, so labels keep font_size however
long or wide the diagram is, and the widget scrolls. In the Viewer pane the
widget fills the pane. In a document with height unset, the widget takes
the diagram's height and scrolls horizontally when the diagram is wider than
the page. fit = TRUE shrinks the diagram to the widget width instead.
Uncolored elements use neutral grays that follow the light or dark theme.
Categorical entities use palette, or the theme palette. node_color
highlights selected nodes: each gets a border in its color and a fill of the
same color at fill_alpha opacity, so its label keeps the theme's text
color. It applies to the nodes of flowchart, state, class, entity
relationship, block, architecture, and kanban diagrams, which Pele draws with
the neutral fill and border.
Supported Mermaid syntax
Pele supports every diagram type built into Mermaid 12.1.0, including 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, and Venn diagrams. The syntax and the per-type differences are listed in Pele's compatibility notes, https://github.com/obsidianmd/pele/blob/1.6.0/website/src/content/docs/compatibility.md; the Mermaid documentation examples of every type, as Mermaid source, are in https://github.com/obsidianmd/pele/tree/1.6.0/tests/corpus.
Not supported: Mermaid themes and themeVariables, ZenUML and other
external Mermaid plugins, HTML labels, and JavaScript callbacks in click
statements. Configuration support varies by diagram type.
Pele computes its own layout, so node positions, spacing, and edge paths
differ from Mermaid's. A flowchart subgraph keeps its own direction when
it links to nodes outside the subgraph. Pele renders at most 50,000 source
characters and 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.
Examples
# Train/test split
draw_mermaid(c(
"flowchart TB",
" data[(Data)] --> split[Split]",
" split --> train[(Training set)]",
" split --> test[(Test set)]",
" train --> fit[Train model]",
" fit -->|trained model| eval[Evaluate on test set]",
" test --> eval"
), node_color = c(
train = rtemis_colors[["teal"]],
test = rtemis_colors[["magenta"]]
))
# Train/test split with hyperparameter tuning by resampling of the
# training set
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 = c(
train = rtemis_colors[["teal"]],
inner_train = rtemis_colors[["teal"]],
val = rtemis_colors[["purple"]],
test = rtemis_colors[["magenta"]]
))
# Nested resampling: the inner loop tunes hyperparameters on each outer
# training set, and the outer loop estimates generalization performance
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 = c(
train = rtemis_colors[["teal"]],
inner_train = rtemis_colors[["teal"]],
val = rtemis_colors[["purple"]],
test = rtemis_colors[["magenta"]]
))
# Pie chart of a data split using a single string for the source
draw_mermaid(
"pie\n \"Train\" : 70\n \"Validation\" : 15\n \"Test\" : 15",
title = "Data split",
palette = c(
rtemis_colors[["teal"]],
rtemis_colors[["purple"]],
rtemis_colors[["magenta"]]
)
)