đ Introduction
An R package is a collection of functions, datasets, documentation, and metadata organized into a standard directory structure. Package development enables developers to create reusable code, share functionality with others, and distribute software through repositories such as CRAN, Bioconductor, or private repositories.
Information
đ¯ Why Develop R Packages?
- Reuse code across multiple projects.
- Share functions with other developers.
- Organize large codebases efficiently.
- Create professional, documented software.
- Distribute applications through package repositories.
đĻ Essential Packages for Development
The following packages simplify the package development workflow.
Installing Development Packages
install.packages("devtools")
install.packages("roxygen2")
install.packages("usethis")
install.packages("testthat")
install.packages("pkgdown")
library(devtools)
library(roxygen2)
library(usethis)
library(testthat)
library(pkgdown)đ Package Development Workflow
đ Standard Package Structure
| Folder/File | Purpose |
|---|---|
| DESCRIPTION | Package metadata. |
| NAMESPACE | Exported functions and imports. |
| R/ | R source code. |
| man/ | Generated documentation. |
| tests/ | Unit tests. |
| data/ | Included datasets. |
| vignettes/ | Tutorials and guides. |
| inst/ | Additional package resources. |
đ Creating a New Package
The usethis package simplifies package creation.
Create a Package
library(usethis)
create_package(
"MyPackage"
)đ Writing Package Functions
Functions are stored as individual .R files inside the R/ directory.
Example Function
addNumbers <- function(
x,
y
) {
x + y
}đ Documenting Functions with roxygen2
roxygen2 generates documentation directly from specially formatted comments.
roxygen2 Documentation
#' Add Two Numbers
#'
#' Adds two numeric values.
#'
#' @param x First number.
#' @param y Second number.
#'
#' @return Sum of x and y.
#'
#' @export
addNumbers <- function(
x,
y
) {
x + y
}đ Generating Documentation
Generate Documentation
library(devtools)
document()đĻ Managing Dependencies
Package dependencies are listed in the DESCRIPTION file.
DESCRIPTION Example
Package: MyPackage
Type: Package
Title: Example Package
Version: 1.0.0
Author: John Doe
Maintainer: John Doe <john@example.com>
Description: Demonstration package.
License: MIT
Imports:
dplyr,
ggplot2đ¤ Exporting Functions
Exported functions become available to users after installing the package.
Export Using roxygen2
#' @export
addNumbers <- function(
x,
y
) {
x + y
}đ§Ē Unit Testing
The testthat package supports automated testing to verify that functions behave as expected.
Creating a Test
library(testthat)
test_that(
"Addition works correctly",
{
expect_equal(
addNumbers(
2,
3
),
5
)
}
)âļ Running Tests
Run All Tests
library(devtools)
test()đ¨ Building the Package
Building creates a distributable package archive.
Build Package
library(devtools)
build()â Checking the Package
Package checking verifies documentation, code quality, examples, and tests.
Check Package
library(devtools)
check()đĨ Installing the Package
Install Local Package
library(devtools)
install()đ Creating a Package Website
The pkgdown package generates a documentation website automatically.
Build Website
library(pkgdown)
build_site()đ Including Data in a Package
Package datasets are typically stored in the data/ directory.
Saving Package Data
studentData <- data.frame(
Name = c(
"Alice",
"Bob"
),
Marks = c(
85,
90
)
)
usethis::use_data(
studentData,
overwrite = TRUE
)đ Creating Vignettes
Vignettes are long-form tutorials that demonstrate package usage.
Create a Vignette
library(usethis)
use_vignette(
"getting-started"
)đ Real-World Example
A data science team develops a package containing reusable data cleaning, visualization, and reporting functions. The package is documented with roxygen2, tested using testthat, and shared internally so that all team members use the same reliable functions across projects.
Reusable Data Cleaning Function
#' Remove Missing Values
#'
#' Removes rows containing missing values.
#'
#' @param data Input data frame.
#'
#' @return Cleaned data frame.
#'
#' @export
cleanData <- function(
data
) {
na.omit(data)
}đ Package Development Lifecycle
đ Common Development Functions
| Function | Purpose |
|---|---|
| create_package() | Creates a new package. |
| document() | Generates documentation. |
| build() | Builds the package archive. |
| check() | Checks package quality. |
| install() | Installs the local package. |
| test() | Runs unit tests. |
| build_site() | Creates a documentation website. |
| use_data() | Adds datasets to a package. |
â ī¸ Common Mistakes
| Mistake | Explanation | Solution |
|---|---|---|
| Missing documentation | Users cannot understand or use package functions effectively. | Document every exported function using roxygen2. |
| Not writing tests | Bugs may remain undetected. | Create automated tests with testthat. |
| Forgetting to export functions | Users cannot access public functions. | Add the @export tag to exported functions. |
| Ignoring package check warnings | May cause installation or compatibility issues. | Resolve all warnings and errors before publishing. |
đĄ Best Practices
- Follow the standard R package directory structure.
- Document every exported function thoroughly.
- Write automated tests for important functionality.
- Run check() before every release.
- Use meaningful version numbers and maintain a changelog.
- Keep functions modular, reusable, and well organized.
- Create vignettes and examples to help users learn the package.
Best Practice
đ Summary
Package development transforms reusable R code into organized, documented, and distributable software. In this chapter, you learned how to create package structures, write functions, document code with roxygen2, manage dependencies, export functions, build and check packages, write unit tests using testthat, include datasets, create package websites with pkgdown, and develop vignettes. Mastering package development enables you to build robust, reusable, and professional R libraries that can be shared with the broader R community or within organizations.