Introduction to R and Rstudio

Session - Packages

Zoë Turner

Package locations

CRAN repository

Comprehensive R Archive Network

19,291 packages (March 2023), free and structurally peer reviewed

GitHub

Many useful packages in development or subject to a lot of change are not on CRAN and are available through GitHub including {NHSRtheme}. These won’t be peer reviewed.

ROpenSci

ROpenSci offer a peer reviewed ecosystem of R packages through GitHub including UKHSA’s {fingertipsR}

Packages

Quotations in R can be either double or single quotes but cannot be mixed or omitted:

# either
install.packages("tidyverse")

# or
install.packages('tidyverse')

or use the RStudio wizard (only for published CRAN packages)

Screenshot of right panels in RStudio with the Packages tab highlighted

Loading package

This needed for every new session/script and is often at the top

library(tidyverse) 

Can be loaded from the packages panel and code appears in the Console

Screenshot of the tick box next to tidyverse in the Packages tab that can be ticked

{tidyverse}

Curly brackets around a {package} is used in text as many packages have “real names”

The tidyverse name in a black screen with coloured dots like stars

What is the {tidyverse}?

The tidyverse package collects (some of) the most popular
R packages into one.

Also refers to a coding style in R.

{tidyverse} debate

Should you use {tidyverse} when many of the packages are redundant?

Clashes occur with the packages which you are notified about, but not with those outside the package

── Attaching core tidyverse packages ────────────────────────────────────────────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.0     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.1     ✔ tibble    3.1.8
✔ lubridate 1.9.2     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ──────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package to force all conflicts to become errors

Errors, warnings & notifications

Warnings - not errors, just information on versions (from an older system)

Warning messages:
1: package ‘tidyverse’ was built under R version 4.0.4 
2: package ‘tidyr’ was built under R version 4.0.3 
3: package ‘readr’ was built under R version 4.0.3 
4: package ‘purrr’ was built under R version 4.0.3 
5: package ‘dplyr’ was built under R version 4.0.3 
6: package ‘stringr’ was built under R version 4.0.3 

Don’t rely upon colour changes as all three are orange in the console!

End session