R’s ecosystem of packages accelerates every data analysis task, from statistical modeling to bioinformatics workflows. The Comprehensive R Archive Network (CRAN), Bioconductor, and GitHub host thousands of ready-to-use libraries—while creating your own package helps you share reusable code. In this post, you’ll learn how to:
Install and update packages from CRAN with
install.packages()andupdate.packages().Explore and install bioinformatics tools via Bioconductor.
Use GitHub as a source for bleeding-edge packages.
Scaffold, document, test, and build your own R package.
By the end, you’ll move seamlessly between consuming community packages and contributing your own.
1. Installing and Updating CRAN Packages
CRAN is R’s official package repository, vetted for quality, compatibility, and platform support. The simplest way to install a package is:
# 1. Install a single package
install.packages("dplyr")
# 2. Load it in your session
library(dplyr)
1.1 Installing Multiple Packages
You can install several packages in one call:
install.packages(c("ggplot2", "readr", "purrr"))
CRAN mirrors are automatically chosen, but you can specify one:
install.packages("data.table", repos = "https://cloud.r-project.org")
1.2 Updating Packages
Over time, package authors release bug fixes, performance improvements, and new features. Keep your library current with:
# Update all out-of-date packages
update.packages(ask = FALSE)
# Update a single package
install.packages("tidyr") # reinstall pulls the latest version
Setting ask = FALSE prevents interactive prompts, making updates scriptable.
2. Exploring Bioconductor for Bioinformatics
Bioconductor specializes in genomic and biological data analysis. Its packages integrate tightly with CRAN but follow a separate release cycle.
2.1 Bioconductor Installation
First, install Bioconductor’s core tools:
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
# Use BiocManager to install bioinformatics packages
BiocManager::install("GenomicRanges")
2.2 Managing Bioconductor Versions
Bioconductor versions align with R releases. Check and update your Bioconductor installation:
# Check your current Bioconductor version
BiocManager::version()
# Upgrade to the latest Bioconductor release
BiocManager::install(version = "3.17")
2.3 Discovering Bioconductor Packages
Browse the online repository at to find tools for:
Sequence analysis (
Biostrings,ShortRead)Differential expression (
DESeq2,edgeR)Single-cell workflows (
SingleCellExperiment,scater)
Installing via BiocManager::install() ensures dependency resolution across CRAN and Bioconductor.
3. Installing Packages from GitHub
GitHub hosts development versions and niche packages not on CRAN. Use devtools or remotes to install directly from a repository.
3.1 devtools::install_github()
install.packages("devtools")
library(devtools)
# Install the latest dplyr development version
install_github("tidyverse/dplyr")
By default, this pulls the master branch. To install a specific tag or branch:
install_github("hadley/ggplot2@v3.4.0")
3.2 remotes::install_github()
The remotes package provides a lightweight alternative:
install.packages("remotes")
remotes::install_github("tidyverse/readr")
Use these functions to test unreleased features or contribute via pull requests.
4. Creating Your Own R Package
Packaging your code turns collections of functions, data, and documentation into a reusable library. The usethis and devtools packages streamline the process.
4.1 Setting Up a Package Skeleton
install.packages(c("usethis", "devtools"))
library(usethis)
# Create a new package in the specified directory
create_package("path/to/myPackage")
This generates:
myPackage/
├─ DESCRIPTION # metadata: Title, Version, Depends
├─ NAMESPACE # functions to export
├─ R/ # directory for your R scripts
└─ man/ # autogenerated documentation
4.2 Filling out DESCRIPTION
Open DESCRIPTION and edit fields:
Package: myPackage
Title: Tools for Custom Data Analysis
Version: 0.1.0
Authors@R: person("Alice", "Smith", email = "alice@domain.com", role = c("aut", "cre"))
Description: Utility functions for data cleaning and visualization.
License: MIT + file LICENSE
Imports:
dplyr,
ggplot2
Imports lists dependencies without attaching them on load.
Depends attaches packages automatically (less common in modern practice).
4.3 Documenting with roxygen2
Write docs above each function using roxygen comments:
#' Calculate the coefficient of variation
#'
#' @param x A numeric vector.
#' @return Numeric value: standard deviation divided by mean.
#' @export
cv <- function(x) {
sd(x, na.rm = TRUE) / mean(x, na.rm = TRUE)
}
Generate documentation and namespace entries:
devtools::document()
This creates .Rd files in man/ and updates NAMESPACE.
4.4 Adding Tests
Include unit tests with testthat:
usethis::use_testthat()
# tests/testthat/test-cv.R
test_that("cv calculates correctly", {
expect_equal(cv(c(2, 4, 6)), sd(c(2,4,6)) / mean(c(2,4,6)))
})
Run tests:
devtools::test()
4.5 Building and Installing Your Package
Build the package tarball:
devtools::build()
Install locally to test:
devtools::install()
Now load with library(myPackage) and try out your functions.
5. Best Practices for Package Development
Use usethis to automate common tasks:
use_mit_license(),use_readme_rmd(),use_code_of_conduct().Include a
README.Rmdthat knits toREADME.mdfor GitHub display.Add vignettes with
usethis::use_vignette()to provide long-form examples.Keep functions single-purpose and well-tested.
Follow the Tidyverse style guide for consistency.
Continuously integrate with GitHub Actions to run tests on every push.
Conclusion
Extending R with packages unlocks community expertise and fosters your own contributions. You now know how to:
Install and update from CRAN and Bioconductor
Pull development versions from GitHub
Scaffold, document, test, and build your own R package
Embrace the package ecosystem to accelerate your workflows and share your code with peers. Ready to dive deeper? In our next chapter, we’ll explore performance tuning and parallel computing to scale R for big data applications. If you have tips on package management or development, share them in the comments below!

Comments
Post a Comment