So far, we’ve only been working with functions built in to a basic install of R. People write a lot of additional code and build or gather a lot of data sets that they package up ready for others to use in R. These bundles of code and data are called packages. Packages add additional functionality to what we can do with R. The central repository for R packages is CRAN – The Comprehensive R Archive Network.

Packages are installed in libraries on your system. Because of this, in R we install packages, but we load libraries to make them available for use. Libraries need to be loaded each time we launch R.

You can see what packages are currently installed on you system with

installed.packages()
##             Package      
## abind       "abind"      
## antiword    "antiword"   
## ape         "ape"        
## aplot       "aplot"      
## AsioHeaders "AsioHeaders"
## askpass     "askpass"    
##             LibPath                                                               
## abind       "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library"
## antiword    "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library"
## ape         "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library"
## aplot       "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library"
## AsioHeaders "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library"
## askpass     "/Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library"
##             Version    Priority Depends       
## abind       "1.4-5"    NA       "R (>= 1.5.0)"
## antiword    "1.3.1"    NA       NA            
## ape         "5.6-2"    NA       "R (>= 3.2.0)"
## aplot       "0.1.9"    NA       NA            
## AsioHeaders "1.22.1-2" NA       NA            
## askpass     "1.1"      NA       NA            
##             Imports                                                                             
## abind       "methods, utils"                                                                    
## antiword    "sys (>= 2.0)"                                                                      
## ape         "nlme, lattice, graphics, methods, stats, tools, utils,\nparallel, Rcpp (>= 0.12.0)"
## aplot       "ggfun (>= 0.0.9), ggplot2, ggplotify, patchwork, magrittr,\nmethods, utils"        
## AsioHeaders NA                                                                                  
## askpass     "sys (>= 2.1)"                                                                      
##             LinkingTo Suggests                      Enhances
## abind       NA        NA                            NA      
## antiword    NA        NA                            NA      
## ape         "Rcpp"    "gee, expm, igraph, phangorn" NA      
## aplot       NA        "ggtree"                      NA      
## AsioHeaders NA        NA                            NA      
## askpass     NA        "testthat"                    NA      
##             License              License_is_FOSS License_restricts_use OS_type
## abind       "LGPL (>= 2)"        NA              NA                    NA     
## antiword    "GPL-2"              NA              NA                    NA     
## ape         "GPL-2 | GPL-3"      NA              NA                    NA     
## aplot       "Artistic-2.0"       NA              NA                    NA     
## AsioHeaders "BSL-1.0"            NA              NA                    NA     
## askpass     "MIT + file LICENSE" NA              NA                    NA     
##             MD5sum NeedsCompilation Built  
## abind       NA     "no"             "4.2.0"
## antiword    NA     "yes"            "4.2.0"
## ape         NA     "yes"            "4.2.0"
## aplot       NA     "no"             "4.2.0"
## AsioHeaders NA     "no"             "4.2.0"
## askpass     NA     "yes"            "4.2.0"

Calculate how many packages are installed on your system.

installed_packages <- installed.packages() # load the contents into a variable

nrow(installed_packages) # print the number of rows

Installing packages

New packages can be installed with

install.packages("package-name")

We need to install ggplot2

install.packages("ggplot2")

Loading packages

Packages can be loaded with

library(package-name)

So, to load ggplot

library(ggplot2)

You will sometimes see people use require() instead of library() to load a package. Generally, either may be used.

You’ll also note that when you install a package, the name is wrapped in quotation marks, " ", but when you load a package, it is not.

Updating packages

You can get a list of any packages with updates available with

old.packages()

And packages can be updated with

update.packages()

Packages provide additional functionality. You can install packages with install.packages() and load them with library().

Function Description
installed.packages print a matrix of installed packages
install.packages Install a new package.
library Load an installed package.
old.packages List packages with updates.
update.packages Update packages to the latest release.