Visualizing Makefiles

[This article was first published on Heidi's stats blog - Rbloggers, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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:

<span class="n">library</span><span class="p">(</span><span class="n">tm</span><span class="p">)</span>
<span class="n">library</span><span class="p">(</span><span class="n">igraph</span><span class="p">)</span>

<span class="c1">## Read makefile into R
</span><span class="n">makefile</span> <span class="o"><-</span> <span class="n">readLines</span><span class="p">(</span><span class="s2">"Makefile"</span><span class="p">)</span>


<span class="c1">## Find relevant lines in makefile
</span><span class="n">dep</span> <span class="o"><-</span> <span class="n">grep</span><span class="p">(</span><span class="s2">":"</span><span class="p">,</span> <span class="n">makefile</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">TRUE</span><span class="p">)</span>

<span class="c1">## Select target files
</span><span class="n">target</span> <span class="o"><-</span> <span class="n">gsub</span><span class="p">(</span><span class="s2">":.*"</span><span class="p">,</span> <span class="s2">""</span><span class="p">,</span> <span class="n">dep</span><span class="p">)</span>

<span class="c1">## Select files target depends on
</span><span class="n">depends</span> <span class="o"><-</span> <span class="n">gsub</span><span class="p">(</span><span class="s2">".*:"</span><span class="p">,</span> <span class="s2">""</span><span class="p">,</span> <span class="n">dep</span><span class="p">)</span>
<span class="n">depends</span> <span class="o"><-</span> <span class="n">strsplit</span><span class="p">(</span><span class="n">depends</span><span class="p">,</span> <span class="s2">" "</span><span class="p">)</span>
<span class="n">names</span><span class="p">(</span><span class="n">depends</span><span class="p">)</span> <span class="o"><-</span> <span class="n">target</span>


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

<span class="c1">## ... and plot
</span><span class="n">plot</span><span class="p">(</span><span class="n">makegraph</span><span class="p">,</span> <span class="n">vertex.shape</span> <span class="o">=</span> <span class="s2">"none"</span><span class="p">,</span> <span class="n">edge.arrow.size</span> <span class="o">=</span> <span class="m">0.5</span><span class="p">)</span>

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)