2.5 Packages
A package is a collection of functions, data sets, and compiled R code for a given purpose (e.g., visualize data, perform specific data analytical techniques). First, we must install the packages on our computer. These packages will be downloaded and stored locally just once. Then, we will upload these packages into our R session when required. The function .libPaths()
shows the directory in which our packages are installed.
.libPaths()
R comes with a standard set of packages (e.g., base, stats). However, most of the time we will use specific packages that include useful functions to organize, model, and visualize our data. The function library()
is used to inspect the packages that we have previously installed on our computer.
library()
Currently (1st May 2023), there are 19,486 different R packages available on CRAN's (i.e., the Comprehensive R Archive Network) repository. These packages are grouped into 42 topics (e.g., psychometrics, graphics, genetics, social sciences, chemometrics and computational physics, econometrics, machine learning, finance, clinical trials, hydrology, medical image analysis, environmetrics, reproducible research).
Some packages' names are self-explanatory (e.g., sem for Structural Equation Models, apaTables for generating APA formatted Tables, Kendall for computing Kendall's nonparametric correlation tests), whereas other names include the abbreviation of specific tests, statistical techniques, or even research methodologies (e.g., WRS2 for Wilcoxon's robust statistics, lavaan for latent variable analysis, sna for social network analysis).
To learn how to install R packages in your machine, please read carefully Appendix A.2. To inspect the list of R packages used on this book and that you will need to install to reproduce all the examples of the book, please read Appendix A.3.
2.5.1 Loading R packages
Some basic packages (e.g., base, stats, graphics) are automatically uploaded when we start our session in R and RStudio. In contrast, most R packages will have to be uploaded at the beginning of our session in order to use them. The functions library()
and require()
load packages, using the name of the package as the first argument of the function. When the packages are successfully uploaded into our session, they are displayed with a ticked box (i.e., active) in the packages tab.
library(magrittr)
require(dplyr)