Visualizing Makefiles

September 15, 2015
By

(This article was first published on Heidi's stats blog - Rbloggers, and kindly contributed to R-bloggers)

I use Makefiles to make reproducibility easier. They can become rather complex.
I lately had the idea that it would be nice to be able to visualize my Makefiles.
Turns out that you need just a few lines in R to get a decent graph. I figured this
would make a nice first blog entry. The code is not super-nice yet and will probably
not work for more complex Makefiles, but you can write me if you have ideas. And
when I have time again, I will implement something for more complex Makefiles.

So here is the code and the visualisation of one of my Makefiles:

library(tm)
library(igraph)

## Read makefile into R
makefile <- readLines("Makefile")


## Find relevant lines in makefile
dep <- grep(":", makefile, value = TRUE)

## Select target files
target <- gsub(":.*", "", dep)

## Select files target depends on
depends <- gsub(".*:", "", dep)
depends <- strsplit(depends, " ")
names(depends) <- target


## Create a dependency matrix (using igraph package)
dlist <- lapply(target, function(t) {
  d <- if(length(depends[[t]]) == 0) NA else depends[[t]]
  data.frame(depends = d, target = t)
  })
dependencymat <- na.omit(do.call("rbind", dlist))
dependencymat <- dependencymat[dependencymat$depends != "", ]                         
makegraph <- graph.data.frame(dependencymat)

## ... and plot
plot(makegraph, vertex.shape = "none", edge.arrow.size = 0.5)

plot of chunk unnamed-chunk-1

Have fun using it!

To leave a comment for the author, please follow the link and comment on their blog: Heidi's stats blog - Rbloggers.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)