# Install the tidyverse package
install.packages("tidyverse")
6 Packages
In Chapter 4, we saw that R comes with many built-in functions (base R, utils and stats package functions). However, the true power of R lies in its extensive ecosystem of additional packages created by the R community. These packages extend R’s capabilities, similar to how third-party apps like WhatsApp, Telegram or Signal extend a phone’s basic messaging capabilities with features like video calls, read receipts, status updates and so on.
6.1 Package Repositories
6.1.1 CRAN (Primary Repository)
Like Apple store and Play store, most R packages are hosted on CRAN (Comprehensive R Archive Network), R’s official package repository. CRAN provides rigorous quality control, ensuring packages meet strict guidelines for reliability and documentation. The tidyverse, for example, is a popular meta-package from CRAN that includes several essential packages for data science like dplyr, ggplot2, and others, which we’ll use extensively in this training.
6.1.1.1 Installing Packages from CRAN
To install packages from CRAN, use install.packages()
:
6.1.2 Alternative Repositories
6.1.2.1 Bioconductor
Bioconductor is a specialized repository focusing on packages for bioinformatics and genomic data analysis. It maintains its own installation system:
# Install Bioconductor's package manager
if (!require("BiocManager", quietly = TRUE))
install.packages("BiocManager")
# Install Bioconductor packages
::install("DESeq2") # Example package for RNA-seq analysis BiocManager
6.1.2.2 GitHub and Other Development Platforms
Many R package developers share their work on platforms like GitHub, GitLab, or Bitbucket before official CRAN submission or for experimental features. To install packages from GitHub, you’ll need the devtools or remotes package:
# Install devtools first if you haven't
install.packages("devtools")
Using devtools
# Install a package from GitHub
# Format: devtools::install_github("username/repository")
::install_github("clauswilke/ggtextures") # Latest the ggtexture package
devtools
# Specify a specific branch, tag, or commit
::install_github("username/repository@branch")
devtools::install_github("username/repository@v1.0.0") # Install a specific version devtools
For package download statistics, visit R Package Stats.
6.2 Using Installed Packages
There are two main ways to use installed packages:
- Use the
library()
function to load all functions from a package:
library(tidyverse)
If you see this error:
Error in library(tidyverse) : there is no package called ‘tidyverse’
It means the package needs to be installed first using install.packages("tidyverse")
.
- To use individual functions without loading the entire package, use the package name followed by
::
and the function name:
::lag() # Use lag() from stats package
stats::lag() # Use lag() from dplyr package dplyr
This method is particularly useful when different packages have functions with the same name (like lag()
and filter()
in dplyr and stats).
6.3 Common Package Installation Errors
When installing R packages, you might encounter various errors. Here’s how to identify and resolve the most common ones:
- . This is common, especially amongst beginners
install.packages(tidyverse)
Error: object 'tidyverse' not found
Fix: - Wait for the auto-complete option and select the package, it automatically inserts the quotation marks. - Manually enclose the package name in quotation marks.
- This typically occurs when R doesn’t have write permissions to the installation directory.
Fix:
- Windows: Run RStudio as administrator
- Linux/Mac:
install.packages("tidyverse", lib = "~/R/library")
Or use sudo R
in terminal (not recommended)
Summary
R packages extend base R’s functionality, providing specialized tools for various analyses. They’re primarily hosted on CRAN and can be installed using install.packages()
. After installation, packages can be used either by loading them entirely with library()
or by accessing specific functions using the package::function
syntax. While installation is permanent, packages must be reloaded in each new R session.