4 Installing R Packages
4.1 What are R packages?
R packages extend the functionality of R by providing additional functions, data, and documentation for specific tasks. They are written by a world-wide community of R users and can be downloaded for free from the internet. 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 the apps you download onto your phone from Apple’s App Store or Android’s Google Play.
Say you have purchased a new phone and you would like to share a recent photo you have taken on Instagram. You need to:
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. You might do this again in the future any time there is an update to the app.
Open the app: After you’ve installed Instagram, you need to open the app.
Once Instagram is open on your phone, you can then proceed to share your photo with your friends. The process is very similar for using an R package. You need to:
- Install the package: 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.
- Load the package: 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.
4.2 Installing R packages from different sources
There are multiple sources of R packages.
1. CRAN
CRAN is the “Comprehensive R Archive Network” - the main repository (or collection) of R packages.
There are three ways to install an R package from CRAN. For example, to install the tidyverse
metapackage (a package of packages):
- Easy way: In the Files pane of RStudio:
- Click on the
Packages
tab - Click on
Install
- Type the name of the package under “Packages (separate multiple with space or comma):” In this case, type
tidyverse
- Click
Install
- Click on the
- Easy way: In the menu bar of RStudio:
- Click on
Tools
andInstall Packages...
- Type the name of the package under “Packages (separate multiple with space or comma):” In this case, type
tidyverse
- Click
Install
- Click on
- Slightly harder way: An alternative way to install a package is by typing
install.packages("tidyverse")
in the Console pane of RStudio and hitting enter. Note you must include the quotation marks.
If you want to update an already installed package to a newer version, you need to re-install it by repeating the above steps.
2. Bioconductor
Bioconductor is a repository for R packages that concern computational biology or bioinformatics. To install the knitr
package from Bioconductor, type the following in the Console of RStudio:
4.3 Loading a package
Recall that after you’ve installed a package, you need to “load” it, in other words open it. We do this by using the library()
command. For example, to load the tidyverse
package, run the following code in the Console pane. What do we mean by “run the following code”? Either type or copy & paste the following code into the Console pane and then hit the enter key.
If after running the above code, a blinking cursor returns next to the >
“prompt” sign, it means you were successful and the tidyverse
package is now loaded and ready to use. If however, you get a red “error message” that reads…
Error in library(tidyverse) : there is no package called ‘tidyverse’
it means that you didn’t successfully install it. In that case, go back to the previous subsection and trying install it again.
EXERCISE 4.3.1
Confirm that you successfully installed the packages ggplot2
, dplyr
and knitr
by loading them.
Package use
One extremely 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
R is telling you that you are trying to use a function in a package that has not yet been “loaded”. Almost all new users forget do this when starting out, and it is a little annoying to get used to. However, you’ll remember with practice.