We at STATWORX work a lot with R and we often use the same little helper functions within our projects. These functions ease our daily work life by reducing repetitive code parts or by creating overviews of our projects. At first, there was no plan to make a package, but soon I realised, that it will be much easier to share and improve those functions, if they are within a package. Up till the 24th December I will present one function each day from
helfRlein. So, on the 5th day of Christmas my true love gave to me…
What can it do?
The aim of this little helper is to visualise the connections between R functions within a project as a flowchart. Herefore, the input is a directory path to the function or a list with the functions and the outputs are an adjacency matrix and an
igraph object. As an example we use this folder with some toy functions:
There are five parameters to interact with the function:
- a path
dirwhich shall be searched.
- a character vector
variationswith the function’s definition string. The default is
patterna string with the file suffix – the default is
- a boolean
simplifythat removes functions with no connections from the plot.
- a named list
all_scriptswhich is an alternative to
dir. This is mainly just used for testing purposes.
For a normal usage it should be enough to provide a path to the project folder.
The given plot shows the connections of each function (arrows) and also the relative size of the function’s code (size of the points). As mentioned above, the output consists of an adjacency matrix and an
igraph object. The matrix contains the number of calls for each function. The
igraph object has the following properties:
- The names of the functions are used as label.
- The numer of lines of each function (without comments and empty one) are saved as the size.
- The folder‘s name of the first folder in the directory.
- A color corresponding to the folder.
With these properties you can improve the network plot for example like this:
Code for example plot
library(igraph) # create plots ------------------------------------------------------------ l
Have a merry advent season!
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…