Revisiting package dependencies

[This article was first published on Revolutions, 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.

by Andrie de Vries

In my previous post I wrote about how to identify and visualize package dependencies.  Within hours, Duncan Murdoch (member of R-core) identified some discrepancies between my list of dependencies and the visualisation.  Since then, I fixed the dispecrancies. In this blog post I attempt to clarify the issues involved in listing package dependencies.

In miniCRAN I expose two functions that provides information about dependencies:

  • The function pkgDep() returns a character vector with the names of dependencies. Internally, pkgDep() is a wrapper around tools::package_dependencies(), a base R function that, well, tells you about package dependencies. My new function is in one way a convenience, but more importantly it sets different defaults (more about this later).
  • The function makeDepGraph() creates an igraph representation of the dependencies.

Take a look at some examples.  I illustrate with the the package chron, because chron neatly illustrates the different roles of Imports, Suggests and Enhances:

  • chron Imports the base packages graphics and stats. This means that chron internally makes use of graphics and stats and will always load these packages.
  • chron Suggests the packages scales and ggplot2. This means that chron uses some functions from these packages in examples or in its vignettes.  However, these functions are not necessary to use chron
  • chron Enhances the package zoo, meaning that it adds something to zoo packages. These enhancements are made available to you if you have zoo installed.

The function pkgDep() exposes not only these dependencies, but also also all recursive dependencies. In other words, it answers the question which packages need to be installed to satsify all dependencies of dependencies.

This means that the algorithm is as follows:

  • First retrieve a list of Suggests and Enhances, using a non-recursive dependency search
  • Next, perform a recursive search for all Imports, Depends and LinkingTo

The resulting list of packages should then contain the complete list necessary to satisfy all dependencies. In code:

> library(miniCRAN)

> tags <- "chron"
> pkgDep(tags, suggests=FALSE, enhances=FALSE, includeBasePkgs = TRUE)
[1] "chron"    "graphics" "stats"   

> pkgDep(tags, suggests = TRUE, enhances=FALSE)
 [1] "chron"        "RColorBrewer" "dichromat"    "munsell"      "plyr"         "labeling"    
 [7] "colorspace"   "Rcpp"         "digest"       "gtable"       "reshape2"     "scales"      
[13] "proto"        "MASS"         "stringr"      "ggplot2"     

> pkgDep(tags, suggests = TRUE, enhances=TRUE)
 [1] "chron"        "RColorBrewer" "dichromat"    "munsell"      "plyr"         "labeling"    
 [7] "colorspace"   "Rcpp"         "digest"       "gtable"       "reshape2"     "scales"      
[13] "proto"        "MASS"         "stringr"      "lattice"      "ggplot2"      "zoo"  

To create an igraph plot of the dependencies, you can use the function makeDepGraph() and plot the results: 

set.seed(1)
plot(makeDepGraph(tags, includeBasePkgs=FALSE, suggests=TRUE, enhances=TRUE), 
     legendPosEdge = c(-1, 1), legendPosVertex = c(1, 1), vertex.size=20)

Note how the dependencies expand to zoo (enhanced), scales and ggplot (suggested) and then recursively from there to get all the Imports and LinkingTo dependencies.

Deps-chron

In my previous post I tried to plot the most popular package tags on StackOverflow.  Using the updated functionality in the miniCRAN functions, it is easier to understand the structure of the dependencies: 

> tags <- c("ggplot2", "data.table", "plyr", "knitr", 
+           "shiny", "xts", "lattice")
> pkgDep(tags, suggests = TRUE, enhances=FALSE)
 [1] "ggplot2"      "data.table"   "plyr"         "knitr"        "shiny"        "xts"         
 [7] "lattice"      "digest"       "gtable"       "reshape2"     "scales"       "proto"       
[13] "MASS"         "Rcpp"         "stringr"      "RColorBrewer" "dichromat"    "munsell"     
[19] "labeling"     "colorspace"   "evaluate"     "formatR"      "highr"        "markdown"    
[25] "mime"         "httpuv"       "caTools"      "RJSONIO"      "xtable"       "htmltools"   
[31] "bitops"       "zoo"          "SparseM"      "survival"     "Formula"      "latticeExtra"
[37] "cluster"      "maps"         "sp"           "foreign"      "mvtnorm"      "TH.data"     
[43] "sandwich"     "nlme"         "Matrix"       "bit"          "codetools"    "iterators"   
[49] "timeDate"     "quadprog"     "Hmisc"        "BH"           "quantreg"     "mapproj"     
[55] "hexbin"       "maptools"     "multcomp"     "testthat"     "mgcv"         "chron"       
[61] "reshape"      "fastmatch"    "bit64"        "abind"        "foreach"      "doMC"        
[67] "itertools"    "testit"       "rgl"          "XML"          "RCurl"        "Cairo"       
[73] "timeSeries"   "tseries"      "its"          "fts"          "tis"          "KernSmooth"  
> set.seed(1)
> plot(makeDepGraph(tags, includeBasePkgs=FALSE, suggests=TRUE, enhances=TRUE), 
+      legendPosEdge = c(-1, -1), legendPosVertex = c(1, -1), vertex.size=10, cex=0.5)

After my previous post, Duncan Murdoch pointed out that the package rgl, suggested by knitr, appeared in the list, but not in the plot.  This new version of the function fixes this bug, which was introduced because I retrieved the suggests dependencies incorrectly:

Deps-so-tags

 EDIT:

A few hours ago the miniCRAN went live on CRAN.  Find miniCRAN at http://cran.r-project.org/web/packages/miniCRAN/index.html

 

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

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)