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.

An R project needs a record of the packages its scripts use. renv gives the project its own package library and records package versions and sources in renv.lock. This separates the project from unrelated packages in your user library.

Start from the project directory and initialize renv with renv::init(). After you change dependencies and verify the analysis, renv::snapshot() updates the lockfile. Review that change before you commit it. Commit renv.lock, .Rprofile, renv/settings.json, and renv/activate.R with the analysis scripts. The renv introduction explains this project setup and the files it creates.

When a collaborator opens the cloned project, its activation script starts renv. renv::restore() installs the package versions recorded in the lockfile. Then rerun the project’s checks and compare its outputs. Do not copy the installed package library between operating systems.

Restoring R packages does not install R itself or every required system library. Record the R and Bioconductor releases as well. A pinned container image can provide the system dependencies that the package lockfile does not record.

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())

The Create a package lesson builds your own package from source files. It connects roxygen2 help, testthat unit tests, package checks, and installation into a separate library.

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.

  • Introduction to renv

    The vignette explains project libraries, lockfiles, snapshots, and restoration. It shows how a project records package versions for a later environment.

  • BiocManager install reference

    The reference describes package selection, updates, and the version argument. These options control which compatible Bioconductor packages the installation requests.

  • BiocManager version guide

    The guide explains compatibility between R and Bioconductor releases. Its validation examples help identify packages from an inconsistent release.

  • R installed.packages reference

    The reference describes package records in selected libraries. A record confirms installation metadata, but does not prove that a package can load.

  • R library reference

    The reference explains namespace loading, attachment, and name conflicts. Compare these operations with the package-presence check before interpreting its result.