Setup
Installing and Using Required Packages
Throughout this tutorial, we’ll use a few essential R packages to manipulate data, run models, and create plots.
Below are the core packages, what they do, and how to install them.
Packages we’ll use:
ggplot2 – For plotting (e.g., scatterplots, regression lines)
dplyr – For data wrangling (filtering, mutating, summarizing, etc.)
gridExtra – To combine multiple plots into one figure
stats – Comes with base R and used for regression (lm())
pacman – Simplifies package management in R
broom (optional) – Makes model summaries easier to work with
Loading Packages
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:igraph':
##
## as_data_frame, groups, union
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
Difference Between install.packages() and library()
- install.packages(“dplyr”) downloads and installs the package — you only need to do this once per computer.
- library(dplyr) loads the package into your R session — you need to run this each time you use it.
Session Information
It’s always good practice to include your session info at the end of your analysis. This gives a snapshot of:
- Your R version and system details
- All the packages that were loaded
- The versions of those packages
This is especially useful when:
- You’re debugging errors
- You’re submitting assignments
- You’re collaborating with others
Why include this?
Sometimes code behaves differently depending on the version of a package or even the version of R itself. Including your session info makes your work reproducible and easier to troubleshoot.
Base R version (simple)
This function comes with R and gives you basic session details.
## R version 4.5.1 (2025-06-13)
## Platform: aarch64-apple-darwin20
## Running under: macOS Sequoia 15.7.4
##
## Matrix products: default
## BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.5-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.1
##
## 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/Los_Angeles
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] gridExtra_2.3 dplyr_1.1.4 ggplot2_3.5.2 igraph_2.2.1 bnlearn_5.1 cobalt_4.6.0 bookdown_0.46
##
## loaded via a namespace (and not attached):
## [1] crayon_1.5.3 vctrs_0.6.5 cli_3.6.5 knitr_1.50 rlang_1.1.6 xfun_0.52
## [7] generics_0.1.4 jsonlite_2.0.0 glue_1.8.0 htmltools_0.5.8.1 sass_0.4.10 scales_1.4.0
## [13] rmarkdown_2.29 grid_4.5.1 jquerylib_0.1.4 evaluate_1.0.4 tibble_3.3.0 fastmap_1.2.0
## [19] yaml_2.3.10 lifecycle_1.0.4 compiler_4.5.1 RColorBrewer_1.1-3 pkgconfig_2.0.3 rstudioapi_0.17.1
## [25] farver_2.1.2 digest_0.6.37 R6_2.6.1 tidyselect_1.2.1 parallel_4.5.1 pillar_1.11.0
## [31] magrittr_2.0.3 bslib_0.9.0 withr_3.0.2 tools_4.5.1 gtable_0.3.6 cachem_1.1.0