Matias Andina
Matias Andina

Reputation: 4230

Script to Flowchart in R

I usually write modular scripts with several functions. When things grow, it's difficult to keep track of what function calls which (naming them like 01-first.R 02-second.R is not always possible and I'd rather not use that as the definitive solution).

Here's an example of a potential script.R that would run 3 "main" functions with a helper.

first <- function(...){
  # do data things
  return(first_output)
}

second <- function(first_output){
  # do data things
  # call helper
  x <- helper(...)
  # do things to x
  return(second_output)
}

third <- function(second_output){
  # do data things
  return(result)
}

I would love to get something like this

enter image description here

Which can be generated within R using the diagrammeR package.

grViz("
digraph boxes_and_circles {

  # a 'graph' statement
  graph [overlap = true, fontsize = 10]

  # several 'node' statements
  node [shape = box,
        fontname = Helvetica]
  first; second; helper; third;

  # several 'edge' statements
  first->second second->helper 
  helper -> second
  second->third 
  third -> result
}
")

Just that (what function calls what other one) would be great. What would be truly awesome is a way to display the types of bifurcations depending on the arguments (e.g, say first has a go_to_third=FALSE by default but if go_to_third=TRUE it jumps directly to third). Having the classes of objects the functions are dealing with would also be great.


I have checked this question Visualizing R Function Dependencies and I wonder whether there are better ways to do this, visually better.


This question is similar to this one in MATLAB Automatically generating a diagram of function calls in MATLAB and I'm OK with a hack using GraphViz from outside R.

Upvotes: 3

Views: 1421

Answers (1)

Rob Challen
Rob Challen

Reputation: 59

Not exactly what you are after but I've been working on a library to visalise dplyr pipelines:

https://github.com/terminological/dtrackr

It can do more or less what you want but at a slightly more granular level. I'd be grateful for any feedback as still experimental.

Upvotes: 2

Related Questions