Installing packages
Some functions, datasets, and tools we will be using don’t come with the default R experience. We need some DLC to access the extra tools we need.
These extra tools are stored in what R calls packages, which you can get delivered in R by using
install.packages("package_name")
The first package we’ll add helps us manage the many packages available to us (how convenient)!
It is called pacman
for “package manager”. Let’s install
it now!
# To install the pacman package, just remove the # at the start of the line below
#install.packages('pacman')
Once you install a package, you won’t need to install it again. If you run the line after the package is installed, all it will do is redownload it, which may waste some time, but it won’t do anything harmful.
Loading Packages
If you want to use the content inside of the package, you’ll need to tell R you want to use it. There are two ways to do this:
- Load all of the tools in the package into R using
library(package_name)
- no quotes around the package name once it’s been installed
library(pacman)
- If you just want a specific function without loading the entire
package, you specify the package then the function by placing
::
between the package name and the function name
package::function()
If you are loading a package into R, you’ll need to reload it at the beginning each time you restart RStudio.
While the package will remain installed, you still need to load the tools inside it into the R environment!
#p_load(pacman, dplyr)
p_load() in pacman
The reason we installed pacman
first is because the
function p_load()
does 2 things:
- It will install the package if it is not installed already
- Then it will load the package into R.
It does both steps in 1 line!
pacman::p_load(dplyr)
Another major bonus of the p_load()
function is you can
list as many packages you want in the same function, where library()
requires you to call the function for each package you want to load.
If we want to load pack1, pack2, and pack3 using
library()
, it would look like:
library(pack1)
library(pack2)
library(pack3)
while using p_load()
p_load(pack1, pack2, pack3)
The next Rmarkdown document will use tidyverse
and
skimr
packages.
Use pacman::p_load()
to install and load both of them
with one line of code:
pacman::p_load(tidyverse, skimr)
Loading needed packages
The code below will install most of the packages we’ll be using in the course.
It may take R Studio some time to install them all, so I recommend running the line below right before you won’t need your computer for a while:
pacman::p_load(tidyverse, broom, gapminder, GGally,
ggrepel, gridExtra, maps, mapproj, mapdata,
quantreg, lindia, rlang, scales, survey, viridis,
viridisLite, usmap, socviz, palmerpenguins,
rpart, rpart.plot, FNN, factoextra)
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.2:
## cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.2/PACKAGES'
## package 'mapdata' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\jmarti52\AppData\Local\Temp\RtmpUBvJAK\downloaded_packages
##
## mapdata installed
## Warning: package 'mapdata' was built under R version 4.2.3
## also installing the dependency 'mitools'
## Warning: unable to access index for repository http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.2:
## cannot open URL 'http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/4.2/PACKAGES'
## package 'mitools' successfully unpacked and MD5 sums checked
## package 'survey' successfully unpacked and MD5 sums checked
##
## The downloaded binary packages are in
## C:\Users\jmarti52\AppData\Local\Temp\RtmpUBvJAK\downloaded_packages
##
## survey installed
## Warning: package 'survey' was built under R version 4.2.3