Advanced R Package Development
Script
source or select+run#commentsPackage
library() anywhere?function| Source | Directory of files with specific structure What you interact with as you build a package |
| Bundle | Compressed into a single file (.tar.gz) via devtools::build() -> R CMD buildVignettes built; files in .Rbuildignore left behind |
| Binary | Platform-specific compressed file (.tgz, .zip) Made with devtools::build(binary = TRUE) → R CMD INSTALL –build |
| Installed | Binary package decompressed into a user’s libraryinstall.packages() |
| In Memory | Loaded and ready for use in an R sessionlibrary() |
We will
We won’t




4.4.0 (4.5 strongly recommended)2025.09 (2026.01 recommended)Opens a new RStudio project with the basic package skeleton:
R/ - directory for your functionsDESCRIPTION - package metadataNAMESPACE - exports (managed automatically)Adds library(devtools) to your .Rprofile so it is always loaded during development.
Not a package dependency - only for your own workflow.
Note: devtools makes all of usethis available too.
Creates R/cpue.R and opens it for editing.
cpue calculates catch per unit effort.
Runs R CMD check - the same check CRAN runs.
Aim for 0 errors, 0 warnings, 0 notes.
Now that we have a basic working function, commit your changes.
Adds LICENSE and LICENSE.md files and updates DESCRIPTION.
Other options: use_gpl3_license(), use_ccby_license(), use_apache_license()
Package: fishr
Title: Calculate standard catch metrics for fisheries
Version: 0.0.0.9000
Authors@R:
person("Jane", "Doe",
email = "jane.doe@something.com",
role = c("aut", "cre"),
comment = c(ORCID = "XXXX-XXXX-XXXX-XXXX"))
Description: Provides functions for calculating and analyzing fisheries
catch data, such as Catch Per Unit Effort (CPUE), a fundamental metric in
fisheries science.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2Insert a template with Ctrl+Alt+Shift+R or Cmd+Option+Shift+R
Title and Description fields in DESCRIPTION@param, @return, and @examplesdocument() and verify ?cpue workscheck() and resolve any warnings#' Calculate Catch Per Unit Effort (CPUE)
#'
#' Calculates CPUE from catch and effort data, with optional gear standardization.
#'
#' @param catch Numeric vector of catch (e.g., kg)
#' @param effort Numeric vector of effort (e.g., hours)
#' @param gear_factor Numeric adjustment for gear standardization (default is 1)
#'
#' @return A numeric vector of CPUE values
#' @export
#'
#' @examples
#' cpue(100, 10)
#' cpue(100, 10, gear_factor = 0.5)
cpue <- function(catch, effort, gear_factor = 1) {
...
}Controls what your package exports and what it imports from other packages.
@export to export a function@importFrom pkg fun to import from another packageCreates README.Rmd. Edit it, then render to README.md:
README.md is what GitHub displays on your repository page.
Commit all new changes to the DESCRIPTION file, documentation and the license.
load_all() vs install()load_all(): during active development - fast, doesn’t install. Use this when working inside your package project.install(): when you want to test as a “real” package, outside your package project.@importFrom?package::function() vs importing?NAMESPACE vs DESCRIPTIONRule of thumb: use package::function() in your code, declare the package under Imports in DESCRIPTION with use_package("pkg").
Run document() regularly - it is fast and keeps man/ files current. check() will catch documentation that does not match the code.
check() outputRun check() early and often. It is much easier to fix one new problem than ten accumulated ones.