| 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