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.
| Source | Description | Install Command |
|---|---|---|
| CRAN | Comprehensive R Archive Network | install.packages() |
| Bioconductor | Bioinformatics packages | BiocManager::install() |
| GitHub | Development versions | devtools::install_github() |
| R-Forge | Collaborative development | install.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
| Package | Purpose |
|---|---|
ggplot2 | Data visualization |
dplyr | Data manipulation |
tidyr | Data tidying |
readr | Fast data import |
purrr | Functional programming |
stringr | String manipulation |
forcats | Factor handling |
tibble | Modern data frames |
Data Science
| Package | Purpose |
|---|---|
caret | Machine learning |
randomForest | Random forests |
xgboost | Gradient boosting |
glmnet | Regularized regression |
cluster | Clustering |
factoextra | Clustering visualization |
Statistics
| Package | Purpose |
|---|---|
stats | Base statistics (built-in) |
MASS | Statistical methods |
survival | Survival analysis |
lme4 | Mixed-effects models |
bayesplot | Bayesian 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 versionlibrary()loads a package ā every session- Tidyverse is a meta-package ā loads core data science packages
- Use
devtoolsto create packages ā modern package development workflow - DESCRIPTION defines package metadata ā name, version, dependencies
devtools::document()generates documentation from roxygen2 commentsdevtools::check()runs comprehensive checks before submission
Next: Learn about R Data Import/Export ā working with various data formats.