The library() function loads an installed R package into the current session, making functions beyond base R available for data import, wrangling, visualization, and analysis. section 1.3 section 2.1
Use library() as part of a practical package workflow:
- Install each package once with install.packages(), then load it whenever a script needs its functions. section 2.1
- Place package-loading calls near the top of a script, before importing data or calling packaged functions. section 1.3 section 3.2
- Load a package collection such as tidyverse to make its included packages available together. section 2.1
- Call library() repeatedly for separate packages, or combine c() with lapply() when many packages are required. section 3.2
Load an analysis toolkit
Install tidyverse once, then load it at the start of a script. Load additional packages when the analysis needs SQL queries, assembled plots, specialized graphics, statistical tests, string handling, data cleaning, or image files. install.packages("tidyverse") library(tidyverse) library(sqldf) library(patchwork) For a larger package set: packages <- c("tidyverse", "networkD3", "patchwork") lapply(packages, library, character.only = TRUE) section 2.1 section 3.2 section 7.1
library(tidyverse) library(reshape2) library(sqldf) library(patchwork)
Inspect the compact install.packages() command before loading tidyverse and using its functions. section 2.1
