Installing and Loading Packages

What is an R Package?

In addition to being open source (i.e. FREE, which is awesome!), there is a large and constantly growing community of people sharing their "R packages" (which is even more awesome!). These packages are collections of scripts that work together to carry out desired routines (or a series of steps). R comes with a "base" package, which gives you the ability to perform many common actions like finding the mean or plotting the histogram of a column of data. However, for specific types of actions we often need to download and install additional packages that others have created and kindly shared online. Though some people only share their packages on their personal websites, most will upload their packages to a common location on the web call the CRAN Archive Network. Within RStudio you have easy access to CRAN via connections through "mirrors" which are locations around the world that grant you access to download these packages. It is highly suggested that you only download packages from CRAN since these packages are routinely updated and screened for problems.

How to Download and Install an R Package

  • Packages menu (0:15)
  • Installing a package (1:06)
  • Loading a package (2:47)
  • Checking what packages are already loaded (3:48)
  • Total length (4:19)

There are three steps to implementing packages in R.

  1. Know the name of the package – sometimes, you will need to google what you are trying to do in order to get the name of the package
  2. Download the package – This can be accomplished in one of two ways:
    • Option a) from within RStudio, click the Packages tab, then click Install, begin to type the name of the package in the middle box, and click install (note that leaving Install Dependences checked will download any additional packages needed to use all capabilities of the package you are currently downloading)
    • Option b) from within the console window or from within a script, run install.packages('package name'). Note: both the period in the function call and the quotation marks around the package name are necessary.
  3. Load, or install, the package – type and run library(package name) from within your script window or as a line in the Console window. Note: quotation marks should NOT be used when calling the library() function.

Be aware that sometimes warning messages will be given in the command window when installing certain packages indicating that the package was built using an older version of R. Except in rare instances, these warnings can be ignored since "older" packages are usually still compatible with newer versions of R.