Skip to content

Instantly share code, notes, and snippets.

@benjamin-chan
Last active July 7, 2016 16:46
Show Gist options
  • Save benjamin-chan/f7dc918e2607b412ccf5 to your computer and use it in GitHub Desktop.
Save benjamin-chan/f7dc918e2607b412ccf5 to your computer and use it in GitHub Desktop.
Create a linear flow diagram
flow <- function (x, switch=NULL) {
# `x` is a character vector of items to diagram
# switch is an optional vector (logical or integer)
# specifying if the element of `x` is run (TRUE or 1) or not (FALSE or 0)
# Usage:
# > flow(c("Part 1", "Part 2", "...", "Part N"),
# + c(TRUE, FALSE, ..., TRUE))
require(DiagrammeR, quietly=TRUE)
require(devtools, quietly=TRUE)
if (packageVersion("devtools") >= "1.12.0") {
source_gist("https://gist.github.com/benjamin-chan/3d569db12bb223e8b3c4",
filename="colorPalette.R",
quiet=TRUE)
} else {
source_gist("https://gist.github.com/benjamin-chan/3d569db12bb223e8b3c4",
quiet=TRUE)
}
require(extrafont, quietly=TRUE)
choose_font(c("Lato", "sans"))
n <- length(x)
alpha <- rep(255, n)
if (!is.null(switch)) {alpha[!switch] <- round(1/4 * 255)}
nodes <- create_nodes(nodes=letters[1:n],
label=x,
style="filled",
fontcolor="white",
fontsize="30pt",
color=paste0(colorPalette()[1], sprintf("%x", alpha)),
shape="oval")
edges <- create_edges(from=letters[1:n-1],
to =letters[2:n])
G <- create_graph(nodes_df=nodes,
edges_df=edges,
graph_name="Processing sequence",
node_attrs="fontname=\"Lato\"",
graph_attrs=c("layout=dot"))
render_graph(G)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment