install.packages("janitor") Packages
What are R packages?
A good analogy for R packages is that they are like apps you can download onto a mobile phone:
- Packages contain additional functions and data that are not built into R
Installing packages
Thee options to install packages:
Use
install.packages()with the package name in quotes
Preferred way: Use
pacman::p_load()with the package namepacman::p_load(janitor)
- The “Packages” tab in Viewer pane

From installing to loading
- Only install packages once (unless you want to update them)
- Installed from Comprehensive R Archive Network (CRAN) = package mothership
- Once packages are installed, we need to load them every time we want to use them in R
- Tip: at the top of your files, have a segment of code dedicated to loading packages
- This way, you can easily see and load packages required for your code
Loading packages
Two options to load packages:
Use
library()with the package name (no quotes!)library(janitor)
Preferred way: Use
pacman::p_load()with the package namepacman::p_load(janitor)
Note: this is the same as installing with p_load()
- That’s because
p_load()will install the package if it is not already installed, and then load it
Packages used in this class
For loading/saving data
herereadrreadxlhavenwritexl
For summarizing/visualizing data
janitorgtrstatixgtsummarynaniarggplot2
For wrangling data
dplyrtidyrtibbleforcatslubridatestringr
Tidyverse
Install and load packages
Note that I am using
pacman::p_load()::is used to access a package’s function without loading the whole package- I am accessing
p_load()from thepacmanpackage
pacman::p_load(
tidyverse,
here,
readxl,
haven,
writexl,
janitor,
rstatix,
gt,
gtsummary,
naniar
)- 1
-
tidyversecontains most of our needed packages - 2
- These are the extra packages we need for loading/saving data
- 3
- These are the extra packages we need for summarizing/visualizing data
References
- Danielle Navarro’s YouTube video on Installing and loading R packages: https://www.youtube.com/watch?v=kpHZVyDvEhQ
- If you want to get more information on packages
- Comprehensive R Archive Network (CRAN) = package mothership
- There is an additional textbook: Hands-On Programming with R by Garrett Grolemund

