11

Are there any tools that can visualize R code?

I have some very complex R code split between several scripts that I need to understand. Part of the difficulty of this is that there are so many nested functions and variables that it's proving extremely slow going to go through it manually and figure out what connects to what.

I think my task might be easier if there were some way to generate a flow chart from R code, so that I can see it all at once. Of course, I can just draw out a map as I go through the code, but shouldn't it be programatically possible to do this? Are there any existing tools that can do it?

I have found a program called R AnalyticFlow, but it seems to MAKE scripts using a GUI, whereas I want something that can take code and make it into the type of thing that one would draw in AnalyticFlow.

Stonecraft
  • 860
  • 1
  • 12
  • 30
  • 2
    For reference, there is a package called `snakefood` that implements this for Python: http://furius.ca/snakefood/ . The graphing you can do (manually) in GraphViz Dot. The answers here might be helpful: https://stackoverflow.com/questions/44143110/visualizing-r-function-dependencies – user5359531 Feb 21 '18 at 21:50
  • That looks cool, I will check it out. If it also can visualize input file dependencies, it's just what I need. – Stonecraft Feb 21 '18 at 21:53
  • 2
    Check out `CodeDepends` , read this vignette and scroll until you see charts : https://cran.r-project.org/web/packages/CodeDepends/vignettes/intro.html – moodymudskipper Feb 27 '18 at 10:12
  • 1
    The `drake` package sounds like it might be relevant for you. It helps generate dependency graphs between your scripts to see what is out of dat.e https://github.com/ropensci/drake – Jon Spring Oct 22 '18 at 14:10
  • Thanks, I will try that. I never did manage to get CodeDepends working, but I still have this need. – Stonecraft Oct 22 '18 at 17:27

2 Answers2

2

You can visualize your functions in a easy way using the mvbills package.

Install/import

install.packages("mvbutils", dependencies = TRUE)
library(mvbutils)

And use

foodweb(where = environment())

In my case, an example of output would be:

enter image description here

Or if it's a package (or you just want to try other option), you can use the DependenciesGraphs package.

Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
1

Even though this question is already a bit old there is a nice R-package called flow that might be very useful for the task:

https://moodymudskipper.github.io/flow/index.html

It graphically visualizes functions, if-then-else statements and more.

shghm
  • 239
  • 2
  • 8