Introduction to using miniCRAN

Building a private repository using makeRepo

Start by creating the recursive dependency tree for your target packages.

For example, imagine a scenario where you want to create a repository that consists of the package foreach and its dependencies.

Start by creating the dependency list:

library(miniCRAN)
# Specify list of packages to download
pkgs <- c("foreach")

revolution <- c(CRAN="http://cran.revolutionanalytics.com")
pkgList <- pkgDep(pkgs, repos=revolution, type="source", suggests = FALSE, )
pkgList
## [1] "foreach"   "codetools" "iterators"

Next, create a repository with the function makeRepo(). In this example, get the required files for source packages as well as windows binaries:

# Create temporary folder for miniCRAN
dir.create(pth <- file.path(tempdir(), "miniCRAN"))

# Make repo for source and win.binary
makeRepo(pkgList, path=pth, repos=revolution, type="source")
## Created new folder: C:\Users\ANDRIE~1\AppData\Local\Temp\Rtmp2FZF00/miniCRAN/src/contrib
makeRepo(pkgList, path=pth, repos=revolution, type="win.binary")
## Created new folder: C:\Users\ANDRIE~1\AppData\Local\Temp\Rtmp2FZF00/miniCRAN/bin/windows/contrib/3.0

Investigate the repository file structure

# List all files in miniCRAN
list.files(pth, recursive = TRUE, full.names = FALSE)
##  [1] "bin/windows/contrib/3.0/codetools_0.2-9.zip"
##  [2] "bin/windows/contrib/3.0/foreach_1.4.2.zip"  
##  [3] "bin/windows/contrib/3.0/iterators_1.0.7.zip"
##  [4] "bin/windows/contrib/3.0/PACKAGES"           
##  [5] "bin/windows/contrib/3.0/PACKAGES.gz"        
##  [6] "src/contrib/codetools_0.2-9.tar.gz"         
##  [7] "src/contrib/foreach_1.4.2.tar.gz"           
##  [8] "src/contrib/iterators_1.0.7.tar.gz"         
##  [9] "src/contrib/PACKAGES"                       
## [10] "src/contrib/PACKAGES.gz"

Use pkgAvail to list available packages in your repository:

# Check for available packages
pkgAvail(repos=pth, type="win.binary")[, c(1:3, 5)]
##           Package     Version Priority      Imports                      
## codetools "codetools" "0.2-9" "recommended" NA                           
## foreach   "foreach"   "1.4.2" NA            "codetools, utils, iterators"
## iterators "iterators" "1.0.7" NA            NA

Using miniCRAN to identify package dependencies

The miniCRAN package exposes two functions that provide information about dependencies:

The package chron neatly illustrates the different roles of Imports, Suggests and Enhances:

A worked example using the package chron

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:

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

library(miniCRAN)
tags <- "chron"
pkgDep(tags)
##  [1] "chron"        "RColorBrewer" "dichromat"    "munsell"     
##  [5] "plyr"         "labeling"     "colorspace"   "Rcpp"        
##  [9] "digest"       "gtable"       "reshape2"     "scales"      
## [13] "proto"        "MASS"         "stringr"      "ggplot2"

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

dg <- makeDepGraph(tags, enhances=TRUE)
set.seed(1)
plot(dg, legendPosition = c(-1, 1), vertex.size=20)

plot of chunk makeDepGraph

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.

An example with multiple input packages

As a final example, create a dependency graph of seven very popular R packages:

tags <- c("ggplot2", "data.table", "plyr", "knitr", "shiny", "xts", "lattice")
pkgDep(tags, suggests = TRUE, enhances=FALSE)
##  [1] "ggplot2"      "data.table"   "plyr"         "knitr"       
##  [5] "shiny"        "xts"          "lattice"      "digest"      
##  [9] "gtable"       "reshape2"     "scales"       "proto"       
## [13] "MASS"         "Rcpp"         "stringr"      "RColorBrewer"
## [17] "dichromat"    "munsell"      "labeling"     "colorspace"  
## [21] "evaluate"     "formatR"      "highr"        "markdown"    
## [25] "mime"         "httpuv"       "caTools"      "RJSONIO"     
## [29] "xtable"       "htmltools"    "bitops"       "zoo"         
## [33] "SparseM"      "survival"     "Formula"      "latticeExtra"
## [37] "cluster"      "maps"         "sp"           "foreign"     
## [41] "mvtnorm"      "TH.data"      "sandwich"     "nlme"        
## [45] "Matrix"       "bit"          "codetools"    "iterators"   
## [49] "timeDate"     "quadprog"     "Hmisc"        "BH"          
## [53] "quantreg"     "mapproj"      "hexbin"       "maptools"    
## [57] "multcomp"     "testthat"     "mgcv"         "chron"       
## [61] "reshape"      "fastmatch"    "bit64"        "abind"       
## [65] "foreach"      "doMC"         "itertools"    "testit"      
## [69] "rgl"          "XML"          "RCurl"        "Cairo"       
## [73] "timeSeries"   "tseries"      "its"          "fts"         
## [77] "tis"          "KernSmooth"
dg <- makeDepGraph(tags, enhances=TRUE)
set.seed(1)
plot(dg, legendPosition = c(-1, -1), vertex.size=10, cex=0.7)

plot of chunk so-tags