R Packages and Libraries — Extending R's Power

R BasicsPackages and LibrariesFree Lesson

Advertisement

R Packages and Libraries — Extending R's Power

Learning Objectives

By the end of this tutorial, you will be able to:

  • Install, load, and manage R packages
  • Understand the CRAN, Bioconductor, and GitHub package ecosystems
  • Use the tidyverse meta-package effectively
  • Create your own R package with devtools
  • Troubleshoot common package issues

What Are Packages?

An R package is a bundle of functions, data, and documentation that extends R's capabilities.

SourceDescriptionInstall Command
CRANComprehensive R Archive Networkinstall.packages()
BioconductorBioinformatics packagesBiocManager::install()
GitHubDevelopment versionsdevtools::install_github()
R-ForgeCollaborative developmentinstall.packages(repos = "http://R-Forge.R-project.org")

Installing Packages

# Install from CRAN
install.packages("ggplot2")

# Install multiple packages
install.packages(c("dplyr", "ggplot2", "tidyr", "readr"))

# Install from GitHub
install.packages("devtools")
devtools::install_github("tidyverse/ggplot2")

# Install from Bioconductor
install.packages("BiocManager")
BiocManager::install("GenomicRanges")

# Install development version
devtools::install_github("user/package", ref = "dev")

Loading Packages

# Load package
library(ggplot2)

# Load with namespace
ggplot2::ggplot()

# See what's loaded
search()
# [1] ".GlobalEnv"        "package:ggplot2"   "package:stats"
# [4] "package:graphics"  "package:grDevices" "package:utils"
# [7] "package:datasets"  "package:methods"   "package:base"

# Detach package
detach("package:ggplot2", unload = TRUE)

Essential Packages

Tidyverse Ecosystem

# Install entire tidyverse
install.packages("tidyverse")

# Load it
library(tidyverse)
# Attaching core packages: ggplot2, tibble, tidyr, readr, purrr, dplyr, stringr, forcats
PackagePurpose
ggplot2Data visualization
dplyrData manipulation
tidyrData tidying
readrFast data import
purrrFunctional programming
stringrString manipulation
forcatsFactor handling
tibbleModern data frames

Data Science

PackagePurpose
caretMachine learning
randomForestRandom forests
xgboostGradient boosting
glmnetRegularized regression
clusterClustering
factoextraClustering visualization

Statistics

PackagePurpose
statsBase statistics (built-in)
MASSStatistical methods
survivalSurvival analysis
lme4Mixed-effects models
bayesplotBayesian analysis

Package Information

# Package details
packageDescription("ggplot2")

# Available functions
ls("package:ggplot2")

# Vignettes
vignette(package = "ggplot2")
vignette("ggplot2-specs")

# Changelog
news(package = "ggplot2")

# Dependencies
tools::package_dependencies("ggplot2")

# Update packages
update.packages()

Creating R Packages

Package Structure

mypackage/
ā”œā”€ā”€ DESCRIPTION        # Package metadata
ā”œā”€ā”€ NAMESPACE          # Exported functions
ā”œā”€ā”€ R/                 # R code files
│   ā”œā”€ā”€ function1.R
│   └── function2.R
ā”œā”€ā”€ man/               # Documentation
│   ā”œā”€ā”€ function1.Rd
│   └── function2.Rd
ā”œā”€ā”€ data/              # Data files
ā”œā”€ā”€ tests/             # Test files
│   └── testthat/
└── vignettes/         # Vignettes

Using devtools

# Install devtools
install.packages("devtools")

# Create package
devtools::create("mypackage")

# Add function
devtools::use_r("function1")

# Add test
devtools::use_testthat()
devtools::use_test("function1")

# Add documentation
devtools::document()

# Build and check
devtools::build()
devtools::check()

# Install locally
devtools::install()

# Run tests
devtools::test()

DESCRIPTION File

Package: mypackage
Title: My Awesome Package
Version: 0.1.0
Authors@R: person("John", "Doe", email = "john@example.com", role = c("aut", "cre"))
Description: Does awesome things with R.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Depends: R (>= 3.5.0)
Imports:
    dplyr,
    ggplot2
Suggests:
    testthat (>= 3.0.0),
    knitr,
    rmarkdown

Troubleshooting

Common Issues

# Package not found
install.packages("package_name")

# Version conflicts
packageVersion("ggplot2")

# Reinstall
remove.packages("ggplot2")
install.packages("ggplot2")

# Load conflicts
conflicts(detail = TRUE)

# Check conflicts
conflicts(filter = c("filter", "lag"))  # dplyr vs stats

# Use namespace to resolve
dplyr::filter()
stats::filter()

Package Dependencies

# See dependencies
tools::package_dependencies("tidyverse")

# Install all dependencies
devtools::install_deps("mypackage")

# Check for missing packages
missing <- setdiff(
  tools::package_dependencies("tidyverse")$tidyverse,
  installed.packages()[, "Package"]
)

Practice Exercises

Exercise 1: Package Explorer

Write a function that lists all functions in a package.

Solution

list_functions <- function(package_name) {
  if (!requireNamespace(package_name, quietly = TRUE)) {
    stop(paste("Package", package_name, "is not installed"))
  }
  ls(paste0("package:", package_name))
}

list_functions("ggplot2")

Key Takeaways

  • install.packages() installs from CRAN — one-time per R version
  • library() loads a package — every session
  • Tidyverse is a meta-package — loads core data science packages
  • Use devtools to create packages — modern package development workflow
  • DESCRIPTION defines package metadata — name, version, dependencies
  • devtools::document() generates documentation from roxygen2 comments
  • devtools::check() runs comprehensive checks before submission

Next: Learn about R Data Import/Export — working with various data formats.

Advertisement

Need Expert R Programming Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement