Section A.4 Packages
Packages are a very important element of
R. Packages are elements that add the functionality of R. What most packages do is they introduce new functions that allow you to ask R to do new different things. Anybody can write a package, so consequently R packages vary on quality and complexity. You can find packages in different places, as well, from official repositories (which means they have passed a minimum of quality control), something called GitHub (a webpage where software developers post work in progress), to personal webpages (danger danger!).
Throughout the book, and hopefully afterwards, you will find yourself installing numerous open-source software packages that allow
R to do new and different things. There are loads of packages out there. In early 2020, there were over 150,000 packages available. Anyone can write one, so you will need to be careful on which ones you use, as the quality can vary. Official repositories, like CRAN, are your best bet for packages, as they will have passed some quality controls.
You can see what packages are available in your local install by looking at the packages tab in the File Directory, Plots, Packages pane.
A number of the packages we will use belong to a set of packages called tidyverse. These packages help make your data tidy. According to Statistician and Chief Scientist at
RStudio, Hadley Wickham, transforming your data into tidy data is one of the most important steps of the data analysis process. It will ensure your data are in the format you need to conduct your analyses. We will also be using the simple features package sf and many more associated with spatial data analysis.
Packages can be installed using the
install.packages() function. Remember that while you only need to install packages once, they need to be loaded with the library() function each time you open up RStudio. Let us install the package dplyr from tidyverse and load it:
library(dplyr)
A lot of code and activity appears in the console. Warnings may manifest. Most of the time, the warnings explain what is being loaded and confirm that the package is successfully loaded. If there is an error, you will have to figure out what the warnings are telling you to successfully load the package. This happens and is normal.
To double-check that you have actually installed
dplyr, go to that File Directory, Plots, Packages pane and click on the Packages tab. The list of packages is in alphabetical order and dplyr should be there. If there is a tick in its box, it means that this package is currently loaded and you can use it; if there is no tick, it means that it is inactive, and you will have to bring it up with library(), or just tick its box.
On masking: sometimes packages introduce functions that have the same name as those that are already loaded into your session. When that happens, the newly loaded ones will override the previous ones. You can still use them, but you will have to refer to them explicitly by bringing them up and specifying to which package they belong with
library().
How do you find out what a package does? You look at the relevant documentation. In the Packages window, scroll down until you find listed the new package we installed. Here you will see the name of the package (
dplyr), a brief description of what the program is about, and the version you have installed (an indication that a package is a good package is that it has gone through several versions, that means that someone is making sure the package gets regular updates and improvements).
Click on the name
dplyr. You will see that RStudio has now brought you to the Help tab. Here is where you find the help files for this package, including all the available documentation.
Every beginner in R will find these Help files a bit confusing. But after a while, their format and structure will begin to make sense to you. Click where it says User Guides, Package Vignettes, and Other Documentation. Documentation in R has become much better since people started to write vignettes for their packages. They are little tutorials that explain with examples what each package does.
