Build a Simple R Package

· 3 min read · Updated March 13, 2026 · advanced
package-development beginner devtools usethis sharing

Ever wanted to share your R functions with colleagues or the community? Turning your code into a package is the best way to do it. An R package bundles your functions into a format that’s easy to install, includes documentation, and works reliably.

This guide shows you the quickest path from zero to a working package you can share.

Why Build a Package?

R packages are more than just a distribution format. They force you to:

  • Write clean, documented code
  • Organize functions logically
  • Test your work systematically
  • Make your code reusable by yourself and others

Even for personal use, packages keep your utility functions organized and accessible.

The Minimal Package in 5 Steps

Here is the fastest way to create a shareable R package.

Step 1: Install Development Tools

You need a handful of packages for package development:

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

Step 2: Create Your Package

Use usethis to set up the package structure automatically:

# Create package in a new directory
create_package("~/myfirstpkg")

This creates a directory with the essential files:

myfirstpkg/
├── DESCRIPTION      # Package metadata
├── NAMESPACE        # What the package exports
├── R/               # Your functions go here
└── README.md        # Introduction

Step 3: Add Your Functions

Open the R/ folder and create a new file. Let us say you have a useful function for calculating confidence intervals:

# R/confint.R

#'\'' Calculate a Confidence Interval
#'\''
#'\'' Computes the confidence interval for a numeric vector.
#'\''
#'\'' @param x A numeric vector
#'\'' @param confidence The confidence level (default 0.95)
#'\'' @return A numeric vector of length 2
#'\'' @export
#'\''
#'\'' @examples
#'\'' ci <- confidence_interval(c(1, 2, 3, 4, 5))
#'\'' ci
confidence_interval <- function(x, confidence = 0.95) {
  se <- sd(x) / sqrt(length(x))
  alpha <- 1 - confidence
  mean(x) + c(-1, 1) * qnorm(1 - alpha / 2) * se
}

The roxygen comments (lines starting with #') become your documentation automatically.

Step 4: Generate Documentation

Run this command to turn your roxygen comments into proper help files:

document()

This creates .Rd files in the man/ directory. You can now access ?confidence_interval after installing.

Step 5: Install and Test

Install your package locally:

install()

Now load it and use your function:

library(myfirstpkg)

# Test your function
confidence_interval(c(10, 12, 14, 16, 18))
# [1]  9.06 18.94

Congratulations — you have built a working R package!

Understanding the DESCRIPTION File

The DESCRIPTION file controls how your package appears to users. Open it and update the essentials:

Package: myfirstpkg
Title: What Your Package Does
Version: 0.1.0
Authors@R: person("Your", "Name", email = "you@example.com", role = c("aut", "cre"))
Description: A short description of what the package does.
License: MIT
Encoding: UTF-8
Imports: 
    stats
RoxygenNote: 7.2.3

Key fields to fill in:

  • Title — a short sentence (not a full description)
  • Description — one or more sentences about what the package does
  • Authors@R — who owns and maintains the package
  • License — MIT, GPL-3, or CC0 are common choices

The Imports field lists packages your code needs to run. Add any packages you use with library() or ::.

Sharing Your Package

GitHub is the standard way to share R packages:

  1. Initialize a git repo: usethis::use_git()
  2. Create a GitHub repo: usethis::use_github()
  3. Push your code

Others can install directly from GitHub:

remotes::install_github("yourusername/myfirstpkg")

Option 2: CRAN

For widespread distribution, submit to CRAN:

  1. Run check() and fix all issues
  2. Read the CRAN submission policies
  3. Submit via the web form

CRAN review takes time but gives your package maximum visibility.

Next Steps

Once you have mastered the basics, consider adding:

  • Tests with testthat: usethis::use_testthat()
  • Vignettes for long-form documentation
  • Data in the data/ directory
  • Unit tests to catch bugs early

See Also