What are R packages?

R packages extend the functionality of R by providing additional functions, data, and documentation. They are written by a worldwide community of R users and can be downloaded for free from the internet.

For example, among the many packages we will use in this course are the ggplot2 package (Wickham, Chang, et al. 2021) for data visualization , the dplyr package (Wickham, François, et al. 2021) for data wrangling , the moderndive package (Kim and Ismay 2021), and the infer package (Bray et al. 2021) for “tidy” and transparent statistical inference.

A good analogy for R packages is they are like apps you can download onto a mobile phone:

So R is like a new mobile phone: while it has a certain amount of features when you use it for the first time, it doesn’t have everything. R packages are like the apps you can download onto your phone from Apple’s App Store or Android’s Google Play.

Let’s continue this analogy by considering the Instagram app for editing and sharing pictures. Say you have purchased a new phone and you would like to share a photo you have just taken with friends on Instagram. You need to:

  1. Install the app: Since your phone is new and does not include the Instagram app, you need to download the app from either the App Store or Google Play. You do this once and you’re set for the time being. You might need to do this again in the future when there is an update to the app.
  2. Open the app: After you’ve installed Instagram, you need to open it.

Once Instagram is open on your phone, you can then proceed to share your photo with your friends and family. The process is very similar for using an R package. You need to:

  1. Install the package: This is like installing an app on your phone. Most packages are not installed by default when you install R and RStudio. Thus if you want to use a package for the first time, you need to install it first. Once you’ve installed a package, you likely won’t install it again unless you want to update it to a newer version.
  2. “Load” the package: “Loading” a package is like opening an app on your phone. Packages are not “loaded” by default when you start RStudio on your computer; you need to “load” each package you want to use every time you start RStudio.

Package Installation

Although you can install packages running a line of R code, it is simpler to follow these instructions. We will install ggplot2.

In the Files pane of RStudio:

  • a. Click on the “Packages” tab.
  • b. Click on “Install” next to Update.
  • c. Type the name of the package under “Packages (separate multiple with space or comma):” In this case, type ggplot2.
  • d. Click “Install.”

Package Loading

Recall that after you’ve installed a package, you need to “load it.” In other words, you need to “open it.” We do this by using the library() command.

For example, to load the ggplot2 package, run the following code in the console pane. What do we mean by “run the following code”? Either type or copy-and-paste the following code into the console pane and then hit the Enter key.

library(ggplot2)

If after running the earlier code, a blinking cursor returns next to the > “prompt” sign in the Console, it means you were successful and the ggplot2 package is now loaded and ready to use. If, however, you get a red “error message” that reads ...

Error in library(ggplot2) : there is no package called ‘ggplot2’

... it means that you didn’t successfully install it. This is an example of an “error message”. Don’t panic if you do get one. If you get this error message, go back to section on R package installation above and make sure to install the ggplot2 package before proceeding.

One very common mistake new R users make when wanting to use particular packages is they forget to “load” them first by using the library() command we just saw. Remember: you have to load each package you want to use every time you start RStudio. If you don’t first “load” a package, but attempt to use one of its features, you’ll see an error message similar to:

Error: could not find function

This is a different error message than the one you just saw on a package not having been installed yet. R is telling you that you are trying to use a function in a package that has not yet been “loaded.” R doesn’t know where to find the function you are using. Almost all new users forget to do this when starting out, and it is a little annoying to get used to doing it. However, you’ll remember with practice and after some time it will become second nature for you.

Attribution: This resource is an adaptation of one in ModernDive by Chester Ismay and Albert Y. Kim.