This is an example how we might integrate the R
package igraph
with the new DiagrammeR
which uses htmlwidgets
to allow us access to the very popular new Javascript library mermaid.js
.
Last active
August 15, 2023 16:10
-
-
Save timelyportfolio/20fc2382a7d30172e4aa to your computer and use it in GitHub Desktop.
R ( igraph + DiagrammeR + pipeR +htmltools ) + JS ( mermaid.js + d3.js + dagre-d3.js )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(igraph) | |
library(pipeR) | |
library(DiagrammeR) | |
library(htmltools) | |
# make a simple graph | |
iG <- graph.formula(Sam+-Mary+-Tom++Jill) | |
# get the edges in a form almost ready for DiagrammeR | |
print( E(iG) ) %>>% | |
unlist %>>% | |
unname %>>% | |
( | |
c( | |
"graph LR" | |
,paste0(gsub(x=.,pattern="(\\s)*(->)(\\s)*",replacement="-->")) | |
) | |
) %>>% | |
paste(collapse=";") %>>% | |
DiagrammeR |
It is not a good idea to get the edge list from the printout, actually, the format of the printout might change. Instead, it is better to query the edge list with get.edgelist()
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Think last line should be
render_diagram