MODULE 2.4 Packages in R

baseR-V2016.2 - Data Management and Manipulation using R

Tested on R versions 3.0.X through 3.3.1
Last update: 15 August 2016


Objective(s):

  • Learn how find, install, load and unload R library packages.

Let’s begin with a question:



What is a package in R?


R packages are collections of functions that perform operations on data, and they are both the blessing and the bane of R. The blessing is packages for any conceivable ecological (or other) analysis are bound to be available, and if not, then you can write one! The bane is finding packages relevant to your work. Visit the R-project.org site for a short description of packages in R. Packages are typically stored in your personal R library.

The number of current packages as of 15 August 2015 was: 8,781. How many are there today? Without worrying about the code per se, run this line in your R console. It returns the number of currently available packages.

# number of current packages at CRAN
  nrow(available.packages()) # number of current R packages 

(The second line of the package site also returns the number of packages currently in the CRAN package repository.)


Finding R Packages Relevant to Your Work


It is easy to find and install R packages if you know the package name. But while some package names are logical, e.g., randomForest() or glm(), others are not, e.g., simba or vegan(). Of the latter two, both actually calculate ecological similarity indices, but the first reminds me of The Lion King while the second of a lifestyle.

Some mechanisms for finding relevant packages include:

  • Literature relevant to your research, and packages cited there.
  • The CRAN Task Views site, which provides general groupings of packages.
  • Google and other web-based searches.

The Literature:
Conscientious researchers will include in a published paper or report the R analytical package(s), as well as proper citation (see Module 2.8) of the package author(s), they have used in their analyses. Depending on your research area, there may even be a generally accepted analytical process a particular package covers. In either case, R packages mentioned in a paper is one of your best sources for packages relevant to your work.

CRAN Task VIews:
Links to packages of possible use to ecologists at the CRAN include Environmetrics and Spatial, among others. Once you are on the CRAN Task Views site, click on Environmetrics, which contains information on R packages relevant to the analysis of ecological data. You can install packages of interest, or install the view itself and all the Task View packages if you desire.

WARNING !! If you opt to install a CRAN view, you will likely install a 100+ separate packages.

Access the Environmetrics and Spatial views, as well as a few of the package links (e.g., vegan in Environmetrics), to obtain a feel how a CRAN view is structured. Of particular use in the package view are the Reference manual (always a .pdf), and the Vignettes. These are excellent starting guides for a package, and you should read them before starting to use a particular package.

Click vegan to see a typical CRAN view of a package.

The Web:
In addition to the CRAN, a search engine like Google can help you find information about packages of relevance to your work, assuming you know the package name(s).

For example, the figure below shows the first three hits of a Google search for the package randomForest, a commonly used classification package in ecology. Note the first is the .pdf for randomForest, the second is the CRAN task View for package, and the third is the Inside-R website (only one of several excellent R-devoted web sites).

It is a good practice to download the package .pdf and read it, and associated vignettes (as above), if any, prior to implementing the package code.


Installing Packages in Your R Library


There are two simple ways to add (install) identified R packages to your R library. (1) The install.packages() call, and (2) via the R GUI dropdowns. When using the install.packages() call, it is recommended you add the dependencies = T option. This automatically installs any additional R packages that are linked to the package currently be installed. Failure to include the dependencies = T option can lead to (non-fatal but) irritating issues during later use of the package.

Once the install begins, you will be asked to select a CRAN repository from where to obtain the package. Select whatever repository makes you happy and fits your native language.

# assume package identified for installation is foreign; package name must be quotes
# NOT RUN
  install.packages("foreign", dependencies = T)   # install package foreign

The second way is from within R. Once (a Plain Vanilla) R session is open, navigate to Toolbar and follow GUI prompts:

  • Packages => Select CRAN repository => select package from pop-up

Installation of packages from with RStudio or other GUI/IDEs is similar, and relies on drop-down GUIs. In all cases installation is largely automatic.


Activating (Loading) and Deactivating (Unloading) a Package in a R Session


The library() call is used to load a package for R session; the search() call returns the currently active packages in your R session. “Loading” a package makes all the functions in the package available, and allows them to be applied to your data.

# load package
  library(foreign)  # load package foreign from library
# find out active packages in current R session
  search()  # list active packages in current R session
##  [1] ".GlobalEnv"        "package:foreign"   "package:stats"    
##  [4] "package:graphics"  "package:grDevices" "package:utils"    
##  [7] "package:datasets"  "package:methods"   "Autoloads"        
## [10] "package:base"

Note that search returns package:foreign, the indication that foreign has been loaded into your current R environment.

To “unload” or “unmask” a currently loaded package use detach(). As a quick exercise, load foreign, call search(), and then detach(package:foreign) and call search() again.

# unload or detach a package
  detach(package:foreign)  # release foreign package
  search()  # package foreign no longer loaded
## [1] ".GlobalEnv"        "package:stats"     "package:graphics" 
## [4] "package:grDevices" "package:utils"     "package:datasets" 
## [7] "package:methods"   "Autoloads"         "package:base"

Updating R Packages


Updates may, or may not be, required, depending on when R was installed on your CPU, and whether the package author(s) have upgraded their package. The default update.packages() returns a query for each out-of-date package. Confirm “yes” or “no” to each package. The option ask = F updates all packages without queries. (Recomendation: always use the ask = F option to avoid answering “yes” to numerous packages in your personal library.)

# update packages
# NOT RUN: examples only
 update.packages(foreign)  # insert specific package inside () if desired
 update.packages(ask = F)  # updates all packages w/out confirmation queries 

Alternatively, you can update from the R window. Place your cursor in toolbar and click Packages. Next click Update Packages.

A cautionary note.

W7/W8/W10 Users often have Administrative permission issues. This is because base R was installed into your C:\Program Files\R versus your personal library of packages being in C:\Users\YourUserName\Documents\R. If the former for the base package in R, update.packages() will return warning that files in C:Fileswere not updated. Just ignore this; updates to base occur only when changing versions. Alternatively, and assuming you have Admin control, you can open R as Administrator and update.


Let’s next install, load, and unload some packages as part of an exercise.


Exercise #1


  • Perform a web search on package reshape, and obtain and file in your system under an appropriate directory the package .pdf
  • Open R and install package reshape on your system using either the GUI from R, or the command-line approach
  • Determine the active packages in your current R session
  • Load reshape into your current R session, and again document packages in your R session; what happens?
  • Unload reshape and call search() again; what happens?
  • Update your existing packages

END MODULE 2.4


Printable Version