Skip to content

Working with Packages

R comes with a set of built-in functions. Packages extend R with thousands of additional functions written by other developers. Think of packages as toolboxes you can add to your workshop.

There are two main sources for R packages:

  • CRAN is the default package repository. It hosts general-purpose packages for statistics, visualization, and data manipulation.
  • Bioconductor is a specialized repository for genomics and bioinformatics packages. Most tools you will use for analyzing sequencing data come from Bioconductor.

Use install.packages() to install a package from CRAN. You only need to install a package once.

install.packages("ggplot2")

To install multiple packages at once, pass a vector of package names.

install.packages(c("dplyr", "readr", "tidyr"))

Bioconductor is the main source for genomics and bioinformatics packages in R. Packages like DESeq2, GenomicRanges, and Biostrings all live here.

First, install the BiocManager package from CRAN. This is a helper package that manages Bioconductor installations.

install.packages("BiocManager")

Then use BiocManager::install() to install Bioconductor packages.

BiocManager::install("DESeq2")

You can install multiple Bioconductor packages at once the same way.

BiocManager::install(c("GenomicRanges", "Biostrings"))

BiocManager::install() also works for CRAN packages. Some bioinformaticians use it as their single installation command for everything.

Installing a package downloads it to your computer. Loading a package makes its functions available in your current R session. Use library() to load a package.

library(ggplot2)

You must load packages every time you start a new R session. Installation is a one-time step. Loading is a per-session step.

You can check whether a specific package is installed on your system.

"ggplot2" %in% rownames(installed.packages())
[1] TRUE
"nonexistent_pkg" %in% rownames(installed.packages())
[1] FALSE

This is useful in scripts where you want to verify dependencies before running an analysis.

Check the version of an installed package with packageVersion().

packageVersion("ggplot2")
[1] '4.0.2'

Knowing your package versions is important for reproducibility. Many bioinformatics papers report the exact versions used in their analyses.

Sometimes two packages define functions with the same name. When you load the second package, its function “masks” the one from the first package. R warns you when this happens.

A common example is filter(). The built-in stats package has a filter() function for time series. The dplyr package also has a filter() function for subsetting data frames. Loading dplyr masks the stats version.

library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union

To use a specific version, prefix the function with the package name and ::. This is called the namespace operator.

df <- data.frame(gene = c("TP53", "BRCA1", "EGFR"), pvalue = c(0.001, 0.5, 0.03))
dplyr::filter(df, pvalue < 0.05)
gene pvalue
1 TP53 0.001
2 EGFR 0.030

Using package::function() syntax removes all ambiguity. It is good practice whenever you know a conflict exists.

To see which packages are currently loaded in your session, use (.packages()).

(.packages())
[1] "dplyr" "ggplot2" "stats" "graphics" "grDevices" "utils"
[7] "datasets" "methods" "base"

The output includes both packages you loaded explicitly and packages that R loads automatically at startup.

Here are some of the most widely used packages in bioinformatics. All are available through Bioconductor unless noted otherwise.

Package Source Description
DESeq2 Bioconductor Differential gene expression analysis from RNA-seq count data
edgeR Bioconductor Differential expression analysis using negative binomial models
GenomicRanges Bioconductor Representation and manipulation of genomic intervals and coordinates
Biostrings Bioconductor Efficient handling of DNA, RNA, and protein sequences
AnnotationDbi Bioconductor Interface to gene annotation databases
Seurat CRAN Analysis of single-cell RNA-seq data
clusterProfiler Bioconductor Gene set enrichment and pathway analysis

You will not need all of these at once. Install them as your projects require them.

As your analyses grow, keeping track of package versions becomes critical. The renv package helps with this. It creates a snapshot of all package versions used in a project. Collaborators can then restore the exact same versions on their machines.

You do not need to learn renv right away. Just know it exists for when you start sharing analyses or publishing results.

Task Command
Install from CRAN install.packages("pkg")
Install from Bioconductor BiocManager::install("pkg")
Load a package library(pkg)
Check if installed "pkg" %in% rownames(installed.packages())
Check version packageVersion("pkg")
Use specific namespace package::function()
List loaded packages (.packages())

Now that you can install and manage packages, you are ready to learn the most popular collection of R packages for data science. Continue to Tidyverse Essentials.