Packages

Author

Nicky Wakim, Jessica Minnier, and Meike Niederhausen

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:

  1. Use install.packages() with the package name in quotes

    install.packages("janitor")   

 

  1. Preferred way: Use pacman::p_load() with the package name

    pacman::p_load(janitor)   
  1. The “Packages” tab in Viewer pane

From installing to loading

 

  • 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:

  1. Use library() with the package name (no quotes!)

    library(janitor)   
  1. Preferred way: Use pacman::p_load() with the package name

    pacman::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

  • here
  • readr
  • readxl
  • haven
  • writexl

For summarizing/visualizing data

  • janitor
  • gt
  • rstatix
  • gtsummary
  • naniar
  • ggplot2

For wrangling data

  • dplyr
  • tidyr
  • tibble
  • forcats
  • lubridate
  • stringr

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 the pacman package
pacman::p_load(
  tidyverse,
  here,
  readxl, 
  haven, 
  writexl,
  janitor,
  rstatix, 
  gt,
  gtsummary, 
  naniar
)
1
tidyverse contains 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