mermaid: R + mermaid.js

Description Usage Arguments Value Examples

View source: R/mermaid.R

Description

Make diagrams in R using mermaid.js with infrastructure provided by htmlwidgets.

Usage

1
mermaid(diagram = "", ..., width = NULL, height = NULL)

Arguments

diagram

diagram in mermaid markdown-like language or file (as a connection or file name) containing a diagram specification. If no diagram is provided diagram = "" then the function will assume that a diagram will be provided by tags and DiagrammeR is just being used for dependency injection.

...

other arguments and parameters you would like to send to Javascript.

width

the width of the resulting graphic in pixels.

height

the height of the resulting graphic in pixels.

Value

An object of class htmlwidget that will intelligently print itself into HTML in a variety of contexts including the R console, within R Markdown documents, and within Shiny output bindings.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
## Not run: 
# Create a simple graph running left to right (note
# that the whitespace is not important)
DiagrammeR("
  graph LR
    A-->B
    A-->C
    C-->E
    B-->D
    C-->D
    D-->F
    E-->F
")
# Create the equivalent graph but have it running
# from top to bottom
DiagrammeR("
   graph TB
   A-->B
   A-->C
   C-->E
   B-->D
   C-->D
   D-->F
   E-->F
")

# Create a graph with different node shapes and
# provide fill styles for each node
DiagrammeR("graph LR;A(Rounded)-->B[Squared];B-->C{A Decision};
 C-->D[Square One];C-->E[Square Two];
 style A fill:#E5E25F;  style B fill:#87AB51; style C fill:#3C8937;
 style D fill:#23772C;  style E fill:#B6E6E6;"
)

# Load in the 'mtcars' dataset
data(mtcars)
connections <- sapply(
 1:ncol(mtcars)
  ,function(i) {
     paste0(
        i
      ,"(",colnames(mtcars)[i],")---"
      ,i,"-stats("
      ,paste0(
        names(summary(mtcars[,i]))
        ,": "
        ,unname(summary(mtcars[,i]))
        ,collapse="<br/>"
      )
      ,")"
   )
 }
)

# Create a diagram using the 'connections' object
DiagrammeR(
   paste0(
     "graph TD;", "\n",
     paste(connections, collapse = "\n"),"\n",
     "classDef column fill:#0001CC, stroke:#0D3FF3, stroke-width:1px;" ,"\n",
     "class ", paste0(1:length(connections), collapse = ","), " column;"
   )
 )

# Also with \code{DiagrammeR()}, you can use tags
# from \code{htmltools} (just make sure to use
# \code{class = "mermaid"})
library(htmltools)
diagramSpec = "
graph LR;
  id1(Start)-->id2(Stop);
  style id1 fill:#f9f,stroke:#333,stroke-width:4px;
  style id2 fill:#ccf,stroke:#f66,stroke-width:2px,stroke-dasharray: 5, 5;
"
html_print(tagList(
  tags$h1("R + mermaid.js = Something Special")
  ,tags$pre(diagramSpec)
  ,tags$div(class="mermaid",diagramSpec)
  ,DiagrammeR()
))

# Create a sequence diagram
DiagrammeR("
sequenceDiagram;
   customer->>ticket seller: ask for a ticket;
   ticket seller->>database: seats;
   alt tickets available
     database->>ticket seller: ok;
     ticket seller->>customer: confirm;
     customer->>ticket seller: ok;
     ticket seller->>database: book a seat;
     ticket seller->>printer: print a ticket;
   else sold out
     database->>ticket seller: none left;
     ticket seller->>customer: sorry;
   end
")

## End(Not run)

Example output

sh: 0: Can't open /dev/null

DiagrammeR documentation built on Sept. 7, 2017, 1:06 a.m.