Skip to main content

13 Extending R with Packages: CRAN, Bioconductor, GitHub, and Your Own Creations

"R packages, CRAN, Bioconductor, GitHub, install.packages, update.packages, devtools, package development"/>  Extending R with Packages: CRAN,

 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() and update.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:

r
# 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:

r
install.packages(c("ggplot2", "readr", "purrr"))

CRAN mirrors are automatically chosen, but you can specify one:

r
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:

r
# 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:

r
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:

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

r
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:

r
install_github("hadley/ggplot2@v3.4.0")

3.2 remotes::install_github()

The remotes package provides a lightweight alternative:

r
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

r
install.packages(c("usethis", "devtools"))
library(usethis)

# Create a new package in the specified directory
create_package("path/to/myPackage")

This generates:

Codice
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:

ini
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:

r
#' 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:

r
devtools::document()

This creates .Rd files in man/ and updates NAMESPACE.

4.4 Adding Tests

Include unit tests with testthat:

r
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:

r
devtools::test()

4.5 Building and Installing Your Package

Build the package tarball:

r
devtools::build()

Install locally to test:

r
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.Rmd that knits to README.md for 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

Popular posts from this blog

Alfred Marshall – The Father of Modern Microeconomics

  Welcome back to the blog! Today we explore the life and legacy of Alfred Marshall (1842–1924) , the British economist who laid the foundations of modern microeconomics . His landmark book, Principles of Economics (1890), introduced core concepts like supply and demand , elasticity , and market equilibrium — ideas that continue to shape how we understand economics today. Who Was Alfred Marshall? Alfred Marshall was a professor at the University of Cambridge and a key figure in the development of neoclassical economics . He believed economics should be rigorous, mathematical, and practical , focusing on real-world issues like prices, wages, and consumer behavior. Marshall also emphasized that economics is ultimately about improving human well-being. Key Contributions 1. Supply and Demand Analysis Marshall was the first to clearly present supply and demand as intersecting curves on a graph. He showed how prices are determined by both what consumers are willing to pay (dem...

“This Sentence Is False”: The Liar Paradox, from Ancient Crete to Modern Code

 “All Cretans are liars,” said the Cretan Epimenides.  “This sentence is false,” echoes every logic textbook.  We’re still arguing 2,600 years later—and the paradox is winning.   _____________________________  /                             \ |   “THIS SENTENCE IS FALSE.”  |  \_____________________________/               |               |  self-reference               v    +---------------------------+    |  Truth flips back on     |    |  itself — paradox loop!  |    +---------------------------+ 1. Meet the Liar The classic one-liner: L: “This sentence is false.” If L is true, then what it asserts—its own falsity—must hold, so L is false. If L is false, then what it asserts isn’t the ca...

Behavioral Portfolio Theory (BPT) – Rethinking Investor Behavior and Portfolio Construction

  Traditional finance theories like Modern Portfolio Theory (MPT) assume that investors are perfectly rational and risk-averse, aiming to maximize utility by optimizing expected returns and variance. However, decades of research in behavioral finance have shown that investors often deviate from purely rational behavior. Behavioral Portfolio Theory (BPT) , introduced by Shefrin and Statman in 2000, offers a fresh perspective by integrating psychological and emotional factors into portfolio construction. What is Behavioral Portfolio Theory? Behavioral Portfolio Theory suggests that investors mentally segment their wealth into multiple “mental accounts” or layers, each with distinct goals, risk preferences, and expectations. Unlike MPT's single-layer approach focusing on an overall risk-return tradeoff, BPT models the portfolio as a layered pyramid , where each layer reflects different investor aspirations. For example: The bottom layer prioritizes capital preservation and safe...