Title: | Create Diagrams with D2 |
---|---|
Description: | Build, read, write, and render diagrams using the D2 syntax. |
Authors: | Eli Pousson [aut, cre, cph] |
Maintainer: | Eli Pousson <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.0.9000 |
Built: | 2024-10-09 05:15:35 UTC |
Source: | https://github.com/elipousson/d2r |
d2_arrowhead()
specifies the label, shape, and fill for a source or target
arrowhead. d2_arrowheads()
allows the specification of both a source and
target arrowhead.
d2_arrowhead( shape = "triangle", type = c("target", "source"), label = NULL, filled = NULL ) d2_arrowheads(..., target = list(), source = list(), id = NULL)
d2_arrowhead( shape = "triangle", type = c("target", "source"), label = NULL, filled = NULL ) d2_arrowheads(..., target = list(), source = list(), id = NULL)
shape |
See |
type |
Type of arrowhead: "target" or "source". |
label |
Short arrowhead label. |
filled |
Must be |
... |
Named parameters used to set values for both the source and target arrowheads. |
target , source
|
Named list of arguments (excluding type) passed to
|
id |
D2 diagram map identifier. All other container or map attributes
are recycled to match the length of |
d2_arrowhead( shape = "cf-many-required", label = "Many required" ) example_diagram <- "A -> B" d2_arrowheads( shape = "triangle", id = example_diagram, filled = FALSE ) d2_arrowheads( source = list( shape = "triangle", label = "source", filled = FALSE ), target = list( shape = "circle", filled = TRUE ), id = example_diagram )
d2_arrowhead( shape = "cf-many-required", label = "Many required" ) example_diagram <- "A -> B" d2_arrowheads( shape = "triangle", id = example_diagram, filled = FALSE ) d2_arrowheads( source = list( shape = "triangle", label = "source", filled = FALSE ), target = list( shape = "circle", filled = TRUE ), id = example_diagram )
A container or map is a group of diagram elements. Some special chart types,
such as the SQL Table created by d2_sql_table()
must be set up as a
container. Using containers can also help set element properties including
the class, label, shape and dimensions (width and height) for a set of
diagram elements.
d2_container( ..., lines = NULL, id = NULL, class = NULL, name = NULL, label = NULL, shape = NULL, width = NULL, height = NULL, icon = NULL, style = d2_style(), collapse = NULL )
d2_container( ..., lines = NULL, id = NULL, class = NULL, name = NULL, label = NULL, shape = NULL, width = NULL, height = NULL, icon = NULL, style = d2_style(), collapse = NULL )
... |
Diagram text. |
lines |
Optional diagram text. If supplied |
id |
D2 diagram map identifier. All other container or map attributes
are recycled to match the length of |
class |
Diagram class |
name |
Diagram container name |
label |
Diagram label |
shape |
Diagram shape |
width , height
|
Diagram width and height (in pixels?) |
icon |
Diagram icon |
style |
A string or results from the |
collapse |
If |
A string or character vector with D2 diagram code.
d2_container( "team1 <-> team2", "team2 <-> team3", "team3 <-> team1", id = c("round1", "round2") ) d2_container( lines = c("shark -> fish: eats", "lion -> gazelle: eats"), id = c("ocean", "savannah") )
d2_container( "team1 <-> team2", "team2 <-> team3", "team3 <-> team1", id = c("round1", "round2") ) d2_container( lines = c("shark -> fish: eats", "lion -> gazelle: eats"), id = c("ocean", "savannah") )
Note: this is an initial experimental implementation of diagram building and may change or be deprecated in the future.
d2_diagram( lines = NULL, ..., connector = NULL, id = NULL, direction = getOption("d2r.direction"), import = NULL, collapse = NULL )
d2_diagram( lines = NULL, ..., connector = NULL, id = NULL, direction = getOption("d2r.direction"), import = NULL, collapse = NULL )
lines |
A list or character vector. Named elements are connected using
the supplied |
... |
Additional elements for diagram included after initial lines. |
connector |
Default to |
id |
Identifier for map container for diagram. |
direction |
Optional direction for diagram. Must be one of: up, down, right, and left. |
import |
File name or names to append as an import to the top of the diagram. If named, the standard import method using the name of the import as the name of the imported element. If import is unnamed, the partial import method is used: https://d2lang.com/tour/imports#partial-imports See: https://d2lang.com/tour/imports |
collapse |
an optional character string to separate the results. Not
|
d2_diagram(c("x" = "y")) d2_diagram(c("a" = "b"), connector = "<-", direction = "up") d2_diagram(c("start" = "end"), connector = "<->", import = "imported.d2")
d2_diagram(c("x" = "y")) d2_diagram(c("a" = "b"), connector = "<-", direction = "up") d2_diagram(c("start" = "end"), connector = "<->", import = "imported.d2")
magick::image_ggplot()
d2_ggplot()
renders a D2 diagram input character vector or file path (to a
diagram file or rendered diagram) and plots the image with
magick::image_ggplot()
.
d2_ggplot( x, ..., density = 150, width = NULL, height = NULL, interpolate = FALSE, arg = caller_arg(x), call = caller_env() )
d2_ggplot( x, ..., density = 150, width = NULL, height = NULL, interpolate = FALSE, arg = caller_arg(x), call = caller_env() )
x |
A character vector of diagram text or a file path for a D2 diagram
file to render as a diagram with |
... |
Additional parameters passed to |
density |
Resolution to render pdf passed to |
width |
in pixels |
height |
in pixels |
interpolate |
passed to ggplot2::annotation_raster |
arg |
An argument name as a string. This argument will be mentioned in error messages as the input that is at the origin of a problem. |
call |
The execution environment of a currently
running function, e.g. |
d2_install()
downloads and executes a sh
script provided for installing
D2 in the Getting Started documentation.
This is not the most secure way to install D2. You should consider using
Homebrew, a pre-built Windows installer, or another installation option
listed in the detailed installation instructions.
d2_install(dry_run = TRUE)
d2_install(dry_run = TRUE)
dry_run |
If |
Read, write, and format D2 files
d2_read(file) d2_write(data, file = NULL, fileext = "d2", tidy = getOption("d2r.tidy", TRUE)) d2_fmt(file)
d2_read(file) d2_write(data, file = NULL, fileext = "d2", tidy = getOption("d2r.tidy", TRUE)) d2_fmt(file)
file |
File name to write to disk. If |
data |
A set of lines or a file path to a d2 file. |
fileext |
File extension to use for writing data to disk. |
tidy |
If |
Render a D2 input file to an output file
d2_include()
combines d2_render()
with knitr::include_graphics()
.
d2_render( input, output = NULL, ..., fileext = "svg", layout = getOption("d2r.layout", "elk"), theme = getOption("d2r.theme"), sketch = getOption("d2r.sketch"), pad = getOption("d2r.pad"), animate_interval = NULL, font_family = NULL, overwrite = TRUE, preview = FALSE, call = caller_env() ) d2_include( input, output = NULL, ..., auto_pdf = getOption("knitr.graphics.auto_pdf", FALSE), dpi = NULL, rel_path = getOption("knitr.graphics.rel_path", TRUE), error = getOption("knitr.graphics.error", TRUE) )
d2_render( input, output = NULL, ..., fileext = "svg", layout = getOption("d2r.layout", "elk"), theme = getOption("d2r.theme"), sketch = getOption("d2r.sketch"), pad = getOption("d2r.pad"), animate_interval = NULL, font_family = NULL, overwrite = TRUE, preview = FALSE, call = caller_env() ) d2_include( input, output = NULL, ..., auto_pdf = getOption("knitr.graphics.auto_pdf", FALSE), dpi = NULL, rel_path = getOption("knitr.graphics.rel_path", TRUE), error = getOption("knitr.graphics.error", TRUE) )
input |
Required input file with |
output |
Output filename. |
... |
Additional input flags. Optional character vector. |
fileext |
One of: svg, pdf, png, gif, pptx. Ignored if |
layout |
Layout. One of: elk, dagre, taga |
theme |
Diagram theme name or code. Theme names are not case sensitive.
Use |
sketch |
If |
pad |
Diagram padding in pixels. Numeric value or string coercible to whole number. |
animate_interval |
Required if fileext is |
font_family |
String with font family name. Passed to
|
overwrite |
If |
preview |
If |
auto_pdf |
Whether to use PDF images automatically when the output
format is LaTeX. If |
dpi |
DPI (dots per inch) value. Used to calculate the output width (in
inches) of the images. This will be their actual width in pixels, divided
by |
rel_path |
Whether to automatically convert absolute paths to relative
paths. If you know for sure that absolute paths work, you may set this
argument or the global option |
error |
Whether to signal an error if any files specified in the
|
Create a D2 SQL Table Diagram
d2_sql_table( data = NULL, visibility = NULL, col_types = vec_ptype_abbr, id = NULL, label = id, ... )
d2_sql_table( data = NULL, visibility = NULL, col_types = vec_ptype_abbr, id = NULL, label = id, ... )
data |
A data frame used for column names and types. Ignored if
|
visibility |
One or more of: , +, -, #. Length is
recycled to match the number of columns in |
col_types |
A named character vector, an unnamed character vector with
the same length as the number of columns in |
id |
Diagram container id passed to |
label |
Diagram label. Defaults to |
... |
Arguments passed on to
|
A character string with D2 diagram code using the "sql_type" shape.
d2_sql_table(mtcars[, 1:4]) d2_sql_table( label = "col_types Example", col_types = c("name" = "char", "num" = "int") )
d2_sql_table(mtcars[, 1:4]) d2_sql_table( label = "col_types Example", col_types = c("name" = "char", "num" = "int") )
Learn more: https://d2lang.com/tour/style
d2_style( ..., inline = FALSE, level = c("shape", "connector", "root"), after = "\n" )
d2_style( ..., inline = FALSE, level = c("shape", "connector", "root"), after = "\n" )
... |
Named attributes for style. Underscores in parameter names are
substituted for dashes to match D2 style, e.g. |
inline |
If |
level |
String. "shape", "connector", or "root". If "root", limit style to supported attributes for root-level style. See https://d2lang.com/tour/style#root for more information. |
after |
String after each line of the style specification. Defaults to
|
d2_style(stroke = "#f4a261", inline = TRUE) d2_style(fill = "green", inline = "tree") d2_style(animated = TRUE, stroke_dash = 3, level = "connector")
d2_style(stroke = "#f4a261", inline = TRUE) d2_style(fill = "green", inline = "tree") d2_style(animated = TRUE, stroke_dash = 3, level = "connector")
Basic D2 utilities
d2_which()
: List path for D2
d2_version()
: List the installed version of D2
d2_themes()
: List available D2 themes
d2_layout()
: Lists available layout engine options with short help
d2_which() d2_version() d2_themes() d2_layout(layout = NULL)
d2_which() d2_version() d2_themes() d2_layout(layout = NULL)
layout |
Layout name. If supplied, display long help for a particular layout engine, including its configuration options. |
is_d2_file()
tests for a ".d2"
file extension. A d2 file extension is not
required in all cases but most functions in this package expect a d2
extension by default.
is_d2_file(file, fileext = "d2")
is_d2_file(file, fileext = "d2")
file |
File name(s) to test. |
fileext |
File extension(s) to test for. |
An incomplete list of standard D2 syntax keywords for reference and internal validation. Named elements include: layout, theme, fileext, connector, arrowhead, direction, style, visibility, constraints, and grid.
keys_d2
keys_d2
A length 10 named list.
A vector of standard D2 theme codes named with theme names.
themes_d2
themes_d2
A length 19 double vector.
https://d2lang.com/tour/themes/