6  Packages

Author

Jarad Niemi

When you install R, you actually install several R packages. When you initially start R, you will load the following packages

You can find this information by running the following code and looking at the attached base packages.

sessionInfo()
R version 4.3.1 (2023-06-16)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.6.1

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: America/Chicago
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] htmlwidgets_1.6.2 compiler_4.3.1    fastmap_1.1.1     cli_3.6.1        
 [5] tools_4.3.1       htmltools_0.5.5   rstudioapi_0.15.0 rmarkdown_2.23   
 [9] knitr_1.43        jsonlite_1.8.7    xfun_0.39         digest_0.6.33    
[13] rlang_1.1.1       evaluate_0.21    

These packages provide a lot of functionality and have been in existence (almost as they currently are) from the early days of R.

While a lot of functionality exist in these packages, much more functionality exists in user contributed packages. On the comprehensive R archive network (CRAN), there are (as of 2023/01/29) 19,122 packages available for download. On Bioconductor, there are an additional 2,183. There are also additional packages that exist outside of these repositories.

6.1 Install packages

To install packages from CRAN, use the install.packages function. For example,

install.packages("tidyverse")

or, to install all the packages needed for this class,

install.packages(c("tidyverse",
                   "gridExtra",
                   "rmarkdown",
                   "knitr",
                   "hexbin"))

R packages almost always depend on other packages. When you use the install.packages() function, R will automatically install these dependencies.

You may run into problems during this installation process. Sometimes the dependencies will fail. If this occurs, try to install just that dependency using install.packages().

Sometimes you will be asked whether you want to install a newer version of a package from source. My general advice (for those new to R) is to say no and instead install the older version of the package. If you want to install from source, you will need Rtools (Windows) or Xcode (Mac). Alternatively, you can wait a couple of days for the newer version to be pre-compiled.

6.1.1 Load packages

The installation only needs to be done once. But we will need to load the packages in every R session where we want to use them. To load the packages, use

library("dplyr")
library("tidyr")
library("ggplot2")

alternatively, you can load the entire (not very big) tidyverse.

library("tidyverse")
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ 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 (<http://conflicted.r-lib.org/>) to force all conflicts to become errors