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:
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:
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:
ggplot2
.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.