Skip to main content

Section 2.3 Using Packages

The Comprehensive R Archive Network (CRAN) is where all of the packages are hosted in R, and when installing packages, this is where they will be installed from.
We briefly spoke about packages in Sectionย 1.8, but now letโ€™s take a deeper dive.

Subsection 2.3.1 Installing Packages

In order to install packages, we can utilize the install.packages() command. Letโ€™s install our first official package, the tidyverse package ([D.1.27]).
install.packages("tidyverse")
Awesome! It may take a second or two for the code to run, but tidyverse has now been installed in R. Importantly, once a package is installed on your local computer, you donโ€™t need to install it again. To get a little more practice, letโ€™s install some more packages that we will be using.
install.packages("palmerpenguins")
install.packages("fortunes")
install.packages("cowsay")

Subsection 2.3.2 Loading Packages

As stated before, once a package has been installed, it does not need to be reinstalled every time you utilize R. However, each time you want to use a package in an R session, you do need to load it. This can be done using the command library().
library(tidyverse)
library(palmerpenguins)
library(fortunes)
library(cowsay)

# Quick sanity check that packages are loaded
sessionInfo()$otherPkgs |> names()
[1] "palmerpenguins" "lubridate"      "forcats"        "stringr"       
 [5] "dplyr"          "purrr"          "readr"          "tidyr"         
 [9] "tibble"         "ggplot2"        "tidyverse"
We have successfully loaded all of the packages (and in turn all of their contents). That means that as long as we do not close this R session, we can utilize the packages.
The palmerpenguins package ([D.1.16]) we will be using in this chapter. Before then, I want to show you that R is fun and diverse. The package fortunes ([D.1.6]) provides you a random R quote with the function fortune() and the say() command from the cowsay package ([D.1.5]) provides a beautiful animal saying whatever you want it to say. Below the animal is โ€œcowโ€ but try some other ones (I suggest โ€œdragonโ€).
# Fun packages
fortunes::fortune()   # random quote about R

cowsay::say("Welcome to tidyverse!", by = "cow")
Running as administrator is like heroin... any problems it solves it replaces
with worse problems.
   -- Jeff Newmiller (about running R CMD check as administrator)
      R-help (February 2013)

 _______________________ 
< Welcome to tidyverse! >
 ----------------------- 
      \
       \

        ^__^ 
        (oo)\ ________ 
        (__)\         )\ /\ 
             ||------w|
             ||      ||
It isnโ€™t always necessary, nor will it be done a lot in this book, but you can use the :: before the command to specifically mention what package the command is coming from. Most of the time this isnโ€™t necessary, but there are times when you will have packages loaded that have overlapping function names. This is when :: should be called.
To further illustrate the diversity of R packages, letโ€™s introduce an example of working with live, real-world data using the nycOpenData package ([D.1.15]). This package connects directly to the NYC Open Data Portal and displays the most recent data happening in the city. By using the nyc_311() function, we can gather the five most recent 311 calls in New York City. The fun thing is that every time you open this chapter, this data will be different! This is an important aspect of working with live data and needs to be considered when creating reproducible research.
First we install the package.
install.packages("nycOpenData")
Now we load it and call the function.
library(nycOpenData)
head(nyc_311())
With some examples under our belt, letโ€™s move to the tidyverse!