| name | roxygen2 Documentation |
| description | Complete guide to roxygen2 tags, Markdown support, cross-references, and documentation patterns for functions, data, and packages |
roxygen2 Documentation
Overview
roxygen2 allows you to write documentation directly above your R code using special comments starting with #'. This skill covers all essential roxygen2 tags, Markdown formatting, and documentation patterns.
Core Concepts
Basic Structure
my_function <- function(x, y) {
}
Required components:
- Title (first line, no blank line before it)
- Blank line (separates title from description)
- Description (optional but recommended)
- Blank line (separates description from tags)
- Tags (@param, @returns, @export, etc.)
Generating Documentation
devtools::document()
roxygen2::roxygenise()
?my_function
Essential Tags
@param
Documents function parameters. One @param per parameter.
Patterns:
- Start with type description ("A numeric vector", "Character string")
- Explain constraints (length, range, allowed values)
- Document defaults if not obvious
- Use
... for dots
@returns (Preferred) / @return
Documents what the function returns. Use @returns (plural) in new code.
my_function <- function(x) {
x * 2
}
my_summarize <- function(data) {
}
my_plot <- function(x) {
plot(x)
invisible(NULL)
}
my_test <- function(x, y) {
list(
estimate = c(1, 2),
std_error = c(0.1, 0.2),
p_value = c(0.01, 0.05)
)
}
Note: @return (singular) is older but still works. @returns is now preferred.
@export
Marks function for export (makes it available to users).
public_function <- function() {
}
internal_helper <- function() {
}
When to export:
- Functions users should call directly
- S3 methods for generics from other packages (often)
- Datasets (automatically exported from data/)
When NOT to export:
- Internal helper functions
- Functions only called by other package functions
- S3 methods for your own generics (use @export with method)
@examples
Provides executable examples.
my_function <- function(x, method = "mean") {
}
Best practices:
- Include comments explaining what each example shows
- Show basic usage first, then advanced
- Demonstrate important edge cases
- Keep examples short and focused
- Examples are run by R CMD check!
@examplesIf
Conditionally run examples (new in roxygen2 7.2.0).
\dontrun{} vs \donttest{}
Control example execution during R CMD check.
Guidelines:
- Prefer runnable examples when possible
- Use
\dontrun{} for interactive code, credentials, web APIs
- Use
\donttest{} for slow operations (>5 seconds)
- Use
@examplesIf instead when appropriate
@importFrom
Imports specific functions from other packages into your namespace.
NULL
Placement:
- Package-level documentation file (R/mypackage-package.R)
- Or in individual function documentation
- Or in a dedicated R/aaa-imports.R file
NULL
@import
Imports entire package namespace (rarely recommended).
NULL
Only use for:
- rlang (if building tidyverse-style package)
- Your own internal utilities package
Avoid for:
- Most packages (use @importFrom or pkg::fun() instead)
- Risk of namespace conflicts
@keywords internal
Marks function as internal (hides from package index).
internal_but_exported <- function() {
}
Use when:
- Function must be exported (e.g., S3 method) but isn't user-facing
- Internal APIs for advanced users
Inheritance and Linking
@inheritParams
Inherits parameter documentation from another function.
base_function <- function(x, y, method = "mean") {
}
wrapper_function <- function(x, y, method = "mean", z) {
base_function(x, y, method)
}
Benefits:
- Reduces duplication
- Ensures consistency
- Easier maintenance
@inherit
Inherits various documentation sections.
original <- function(x) {
}
modified <- function(x) {
}
Can inherit:
@inherit fun params - all parameters
@inherit fun return - return value
@inherit fun examples - examples
@inherit fun description - description
@inherit fun details - details
@inherit fun - everything
@family
Creates "See Also" links to related functions.
mean_wrapper <- function(x) {
mean(x)
}
median_wrapper <- function(x) {
median(x)
}
mode_wrapper <- function(x) {
}
Generates links: "Other statistical functions: median_wrapper(), mode_wrapper()"
@seealso
Manual cross-references and external links.
@rdname
Combines documentation of multiple functions on one page.
get_config <- function(key) {
pkg_env$config[[key]]
}
set_config <- function(key, value) {
pkg_env$config[[key]] <- value
invisible(value)
}
Generates single help page: ?config shows both functions.
Use for:
- Getter/setter pairs
- Function families that are always used together
- Different interfaces to same functionality
@describeIn
Documents multiple related items with individual descriptions.
process <- function(x) {
UseMethod("process")
}
process.numeric <- function(x) {
}
process.character <- function(x) {
}
process.data.frame <- function(x) {
}
Generates: Single page with subsections for each method.
Markdown Support
Enable in DESCRIPTION:
Roxygen: list(markdown = TRUE)
Text Formatting
Lists
Links
Auto-linking:
[function_name()] - links to function in same package
[pkg::function_name()] - links to function in other package
- Backticks alone don't create links:
function_name
Sections
Data Documentation
Document datasets in R/data.R:
"who"
Key tags for data:
@format - describe structure
\describe{} and \item{} - describe columns/elements
@source - where data came from
- Quote the dataset name:
"dataset_name"
Different Data Types
"example_vector"
"example_list"
"example_matrix"
Package-Level Documentation
Use "_PACKAGE" for package-level documentation:
"_PACKAGE"
Generates: ?mypackage help page.
Common pattern: Put package-level @importFrom here.
Advanced Patterns
S3 Method Documentation
print.myclass <- function(x, ...) {
cat("myclass object with", length(x), "elements\n")
invisible(x)
}
summary.myclass <- function(x, ...) {
structure(
list(n = length(x), mean = mean(x)),
class = "summary_myclass"
)
}
Documenting Multiple Related Functions
NULL
add <- function(x, y) x + y
subtract <- function(x, y) x - y
multiply <- function(x, y) x * y
divide <- function(x, y) x / y
Templates with @template
Create reusable documentation chunks:
my_function <- function(verbose = FALSE, ...) {
}
Setup:
- Create
man-roxygen/ directory
- Put
.R files with roxygen blocks
- Use
@template filename (without .R)
Complete Templates
Exported Function
my_function <- function(x, method = "mean", na.rm = FALSE,
verbose = FALSE, ...) {
}
Internal Function
internal_helper <- function(x) {
}
Note: @noRd means "don't create .Rd file" - use for purely internal functions.
Dataset
"example_data"
Package Documentation
"_PACKAGE"
Common Pitfalls
1. Missing Blank Line After Title
2. Using @return Instead of @returns
Not wrong, but @returns is now preferred:
3. Forgetting @export
my_function <- function() {}
my_function <- function() {}
4. Not Documenting All Parameters
R CMD check will warn if parameters lack documentation:
my_function <- function(x, y) {}
my_function <- function(x, y) {}
5. Broken Links
6. Forgetting @format for Data
"dataset"
"dataset"
7. Using \ Escapes Unnecessarily
With Markdown enabled, use Markdown syntax:
8. Examples That Don't Run
9. Missing @keywords internal for Internal Exports
internal_but_exported <- function() {}
10. Not Running devtools::document()
devtools::document()
roxygen2::roxygenise()
Quick Reference
Essential Tags
Markdown Quick Reference
Resources