Here is the code used to generate the code flow diagram.
Two packages are required.
require(igraph)
require(rCharts)
Project file names can be loaded using pattern
arguments with list.files
pointing to both the code
and relevant docs
directories. However, there is a substantial amount of hardcoding involved for any project as shown here. For example, the rpm.R
script contains all the rpm
functions. Although this script has a complete, corresponding rpm.Rmd
file, I decided to subsequently break out the functions and other content from rpm.Rmd
into multiple Rmd files. Such ad hoc project-specific decisions require that I take note here and make the related distinctions.
setwd("C:/github/ProjectManagement/docs/Rmd")
c0 <- list.files("../../code", pattern = ".R$")
c1a <- c("_output.yaml", "navbar.html", "leonawicz.github.io")
c1b <- "supporting libraries"
c1c <- "in_header.html"
c2 <- list.files(pattern = ".Rmd$")
rmd.primary <- which(gsub(".Rmd", ".R", c2) %in% c0)
c2a <- c2[rmd.primary]
c2b <- c2[-rmd.primary]
c3 <- gsub(".Rmd", ".md", c2)
c4 <- gsub(".Rmd", ".html", c2)
Directional connections must be made among files. The connections are expressed by element-wise comparison of the equal-length to
and from
vectors.
from <- c(
c0, rep("rpm.Rmd", length(c2b)), c2, c2, rep("pm.R", length(c1a)), c1b, rep(c(c1a, c1c), each=length(c4))
)
to <- c(
c2a, c2b, c3, c4, c1a, "pm.R", rep(c4, length(c(c1a, c1c)))
)
The vectors are combined in a data frame and the igraph
package is used to grow the tree diagram.
relations <- data.frame(from = from, to = to)
g <- graph.data.frame(relations, directed = T, vertices = data.frame(c(c0, c1a,
c1b, c1c, c2, c3, c4)))
gw <- get.data.frame(g)
gw$value <- 1
colnames(gw) <- c("source", "target", "value")
gw$source <- as.character(gw$source)
gw$target <- as.character(gw$target)
The rcharts
package has functionality for turning this into an interactive D3 visualization, which is nice, particularly the mouseover interactivity, since there can be so much visual overlap among files for complex projects. Additional javascript can be included to alter the colors. My strengths are in R so I borrowed this code snippet from online, but if you have skills with javascript and D3 you could probably do better with color control and opacity I imagine.
p <- rCharts$new()
p$setLib("http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey")
p$setTemplate(script = "http://timelyportfolio.github.io/rCharts_d3_sankey/libraries/widgets/d3_sankey/layouts/chart.html")
p$set(data = gw, nodeWidth = 15, nodePadding = 10, layout = 32, width = 900,
height = 800, margin = list(right = 20, left = 20, bottom = 50, top = 50),
title = "Code Flow")
p$setTemplate(afterScript = "\n<script>\n var cscale = d3.scale.category20b();\n d3.selectAll('#{{ chartId }} svg path.link')\n .style('stroke', function(d){\n return cscale(d.source.name);\n })\n d3.selectAll('#{{ chartId }} svg .node rect')\n .style('fill', function(d){\n return cscale(d.name)\n })\n .style('stroke', 'none')\n</script>\n")
Embed the chart in a document when rendering.
p$show("iframesrc", cdn=T)