Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active August 15, 2023 16:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/20fc2382a7d30172e4aa to your computer and use it in GitHub Desktop.
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 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.

code

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
@pssguy
Copy link

pssguy commented Dec 30, 2014

Think last line should be
render_diagram

@gaborcsardi
Copy link

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