1.2 R Packages

Packages are modules of R code that enhance the capabilities of R. Many packages are well established and curated, and have to go through a strict software compatibility review before allowed on CRAN.

Installing packages

Installing packages on your computer can be done from the RStudio menu (Tools > Install Packages), or by running the command install.packages(<package name>, repos = "https://cran.rstudio.com"). For example, to install the readxl package, which we will use shortly, we would run the code

install.packages("readxl", repos="https://cran.rstudio.com")

You can set the default repository in RStudio, in Tools > Global Options.

Be aware that everything here is case-sensitive

Another way to install packages is to go to the Packages pane in RStudio, use the search bar there to find the package you want to install, and then click the checkbox beside the name. This is convenient, but not very reproducible if you have to move to a different computer, so it’s generally discouraged.

How do you find packages? Glad you asked. The easiest way to find packages on CRAN is actually through the RStudio Packages pane, where the entire set of available packages are listed with a brief, top-line description. You can click on the package name to see a much more detailed overview of the packages, and many packages do have vignettes which give more information. However, once you’ve found the package you want, you should really code it up with install.packages, so that you can save the script for later when you might need to remember it again.

1.2.1 Loading packages in R

We will use several packages to enhance our experience and get going faster. However, to use a package, you must first load it into R. To load a package into R, you use the function library (ironically).

The first package we will load is the tidyverse package. This is actually a meta-package, which in turn loads a bunch of other packages. These form a core group of useful packages that are widely used, including

  • readr (reading data from text files)
  • tidyr (Manipulation, pivoting)
  • dplyr (summarize, aggregate, filter)
  • ggplot2 (visualization)
  • purrr (functions applied across data structures, meta-programming)
  • stringr (string manipulation)
  • forcats (categorical data)

In addition, we’ll load the readxl package for reading Excel files.

library(tidyverse)
library(readxl)