Typically, we rely on a number of external packages when programming in R. This document explores how those packages are installed and loaded.

When R is installed, it stores a number of paths (i.e. directories or folders) in its internal memory. These can be accessed with certain R functions.

For example, a key path is the “home” directory where the core R files live. You can find this path by running R.home():

R.home()
## [1] "/Library/Frameworks/R.framework/Resources"

You can then see which files and folders live here with the dir() function:

dir(R.home())
##  [1] "bin"          "COPYING"      "doc"          "etc"         
##  [5] "fontconfig"   "include"      "Info.plist"   "lib"         
##  [9] "library"      "man1"         "modules"      "R"           
## [13] "Rscript"      "share"        "SVN-REVISION" "tests"

The folder structure may be familiar to Linux/Unix users. If not, that’s fine. We’re not concerned with the internal mechanics of R itself. Instead, we’ll focus on what happens with installed packages.

The command .libPaths() will print the folder(s) where R packages are downloaded (with install.packages()) or searched (with library()):

.libPaths()
## [1] "/Users/gfiddyment/Library/R/3.3/library"                       
## [2] "/Library/Frameworks/R.framework/Versions/3.3/Resources/library"
my.path = .libPaths()[1]

Searching the folders in my.path will show one folder per installed package. We’ll look inside the folder for library(car):

dir(my.path)
##   [1] "abind"           "acepack"         "acs"            
##   [4] "akima"           "alr4"            "bdsmatrix"      
##   [7] "bindr"           "bindrcpp"        "bnlearn"        
##  [10] "car"             "caret"           "caTools"        
##  [13] "checkmate"       "choroplethr"     "coefplot"       
##  [16] "combinat"        "corrplot"        "coxme"          
##  [19] "ctv"             "data.table"      "DataCombine"    
##  [22] "devtools"        "doParallel"      "dplyr"          
##  [25] "dygraphs"        "e1071"           "effects"        
##  [28] "fivethirtyeight" "Formula"         "gam"            
##  [31] "gbm"             "geosphere"       "GGally"         
##  [34] "ggmap"           "ggplot2"         "ggplot2movies"  
##  [37] "ggvis"           "glmnet"          "glue"           
##  [40] "gmp"             "gridBase"        "HH"             
##  [43] "Hmisc"           "hms"             "htmltab"        
##  [46] "htmlTable"       "igraph"          "igraphdata"     
##  [49] "irlba"           "ISLR"            "jpeg"           
##  [52] "klaR"            "lattice"         "latticeExtra"   
##  [55] "leaps"           "lm.beta"         "lmtest"         
##  [58] "mapproj"         "maps"            "mAr"            
##  [61] "MASS"            "minqa"           "mnormt"         
##  [64] "multcomp"        "mvtnorm"         "neuralnet"      
##  [67] "NMF"             "packrat"         "perturb"        
##  [70] "pkgconfig"       "pkgmaker"        "PKI"            
##  [73] "plogr"           "pls"             "plyr"           
##  [76] "png"             "polspline"       "prettyunits"    
##  [79] "pROC"            "profvis"         "progress"       
##  [82] "proto"           "pryr"            "psych"          
##  [85] "randomForest"    "RCurl"           "readr"          
##  [88] "registry"        "reshape"         "rgl"            
##  [91] "RgoogleMaps"     "rJava"           "rjson"          
##  [94] "RJSONIO"         "rlang"           "rmarkdown"      
##  [97] "Rmpfr"           "rms"             "rngtools"       
## [100] "ROCR"            "rpart"           "rpart.plot"     
## [103] "rsconnect"       "rstan"           "sand"           
## [106] "sandwich"        "sp"              "StanHeaders"    
## [109] "survey"          "survival"        "TH.data"        
## [112] "tibble"          "titanic"         "tree"           
## [115] "useful"          "vcd"             "VGAM"           
## [118] "viridis"         "WDI"             "xlsx"           
## [121] "xlsxjars"        "XML"
dir(paste(my.path, '/car', sep=''))
##  [1] "CITATION"    "data"        "DESCRIPTION" "doc"         "help"       
##  [6] "html"        "INDEX"       "Meta"        "NAMESPACE"   "NEWS"       
## [11] "R"

We see there are sub-folders for data, documentation, and R functions:

dir(paste(my.path, '/car/data', sep=''))
## [1] "Rdata.rdb" "Rdata.rds" "Rdata.rdx"
dir(paste(my.path, '/car/doc', sep=''))
## [1] "embedding.pdf" "embedding.R"   "embedding.Rnw" "index.html"
dir(paste(my.path, '/car/R', sep=''))
## [1] "car"     "car.rdb" "car.rdx"

This is the typical setup, but there are also tools for making “environments” (i.e. folders with all code and dependent R packages together) that can easily be used across different computers. The main such tool is library(packrat). See http://rstudio.github.io/packrat/walkthrough.html for an introduction.