01 / 21
#'

Function
Documentation in R

Learn to write clear, structured documentation for your R functions using comments and roxygen2-style tags so others (and future you) can understand your code.

R Programming

Why Document Your Functions?

When you write a function in R, it might work perfectly today. But what about six months from now? What about when a classmate tries to use it? Documentation is the practice of adding structured descriptions to your code so that anyone can understand what a function does, what it expects, and what it returns.

Key Terms to Know

Comment
A line starting with # that R ignores when running code: purely for human readers.
Roxygen Tag
A special comment starting with #' followed by a keyword like @param or @return.
Help Page
The documentation you see when you type ?mean in R: roxygen tags generate these automatically.

Plain Comments vs Roxygen Style

Plain Comments # Calculate mean of x # x: numeric vector # returns: single number my_mean <- function (x) { sum (x) / length (x) } ✕ No standard format ✕ Not machine-readable Roxygen Style #' Calculate mean of x #' @param x numeric vector #' @return single number my_mean <- function (x) { sum (x) / length (x) } ✓ Standardized format ✓ Generates help pages

The Core Roxygen Tags

1
Title line: The very first #' line is the function's title: a short, one-line summary.
2
@description: A longer paragraph explaining what the function does and when to use it.
3
@param name: Documents one parameter: what type it should be and what it represents.
4
@return: Describes what the function gives back: its type and meaning.
5
@examples: Shows runnable code demonstrating how to call the function.

Anatomy of a Documented Function

#' Convert Celsius to Fahrenheit #' @description Converts a #' temperature from C to F. #' @param celsius numeric value in C #' @return numeric value in F #' @examples #' to_fahrenheit(100) to_fahrenheit <- function (celsius) { celsius * 9/5 + 32 } Title Line @description @param @return @examples Function Body

Inline vs Block Comments

Inline Comments Roxygen Block
Syntax # comment #' @tag text
Placement Anywhere in code Directly above function
Purpose Explain how code works Explain what function does
Machine-readable No Yes
Generates help? No Yes (via roxygen2)

How to Write Documentation

1
Start with the title: Write one clear sentence describing the function's purpose on the first #' line.
2
Add @param for each argument: State the expected type (numeric, character, logical) and what it represents.
3
Add @return: Describe the output type and meaning. Be specific: "a numeric vector of length 1" is better than "a number."
4
Add @examples: Include at least one runnable call with expected output to show usage.
5
Review and test: Read your docs as if you've never seen the function. Can you use it without reading the code?

Example 1: Simple Function

#' Calculate the area of a circle #' #' @param radius numeric. The radius of the circle. #' @return numeric. The area (\(\pi \cdot r^2\)). #' @examples #' circle_area(5) # returns 78.53982 circle_area <- function(radius) { pi * radius^2 }

Example 2: Multiple Parameters

#' Calculate body mass index (BMI) #' #' @description Computes BMI from weight #' in kilograms and height in meters. #' #' @param weight_kg numeric. Weight in kilograms. #' @param height_m numeric. Height in meters. #' @return numeric. BMI value (\(kg/m^2\)). #' @examples #' calc_bmi(70, 1.75) # returns 22.86 calc_bmi <- function(weight_kg, height_m) { weight_kg / height_m^2 }

Example 3: Defaults and Edge Cases

#' Standardize a numeric vector #' #' @description Subtracts the mean and #' divides by SD. Optionally removes NAs. #' #' @param x numeric vector to standardize. #' @param na.rm logical. Remove NAs? Default TRUE. #' @return numeric vector with \(\text{mean} = 0\) and \(\text{SD} = 1\). #' @examples #' standardize(c(2, 4, 6)) #' standardize(c(1, NA, 3), na.rm = TRUE) standardize <- function(x, na.rm = TRUE) { (x - mean(x, na.rm = na.rm)) / sd(x, na.rm = na.rm) }

Bonus Tags: @export and @import

@export
Makes the function available to users of your package. Without it, the function stays internal (hidden).
@import
Declares that your function depends on another package, e.g., @import dplyr or @importFrom stats lm.

How Roxygen Becomes a Help Page

Your .R File with #' tags roxygen2 .Rd File auto-generated R CMD ?your_fn Help Page YOU WRITE AUTO-GENERATED USER SEES

Weak vs Strong Documentation

Weak
#' Does stuff
#' @param x the input
#' @return result

Vague, no types, no context. A reader learns almost nothing.
Strong
#' Remove outliers from a vector
#' @param x numeric vector
#' @param threshold numeric. SD cutoff. Default 2.
#' @return numeric vector with outliers removed.

Specific, typed, default noted.

Common Mistakes

✕ Missing @param
Every parameter in the function signature must have its own @param tag. Skipping one makes the docs incomplete.
✕ No type info
Writing "@param x the data" tells you nothing. Always state the type: numeric, character, data.frame, etc.
✕ Using # instead of #'
Regular comments (#) are invisible to roxygen2. You must use the #' prefix for documentation to be parsed.

Tag Reference Summary

Tag Purpose Required?
Title line One-sentence summary Yes
@description Detailed explanation Recommended
@param Document each argument Yes
@return Describe return value Yes
@examples Show usage with code Recommended
@export Make public in package Package only

Question 1 of 5

What character sequence starts a roxygen-style documentation comment in R?

Question 2 of 5

Which roxygen tag is used to describe a function parameter?

Question 3 of 5

Which line should fill the blank to complete this documentation?

Question 4 of 5

A classmate's @param line reads: #' @param x the input. What is the main problem?

Question 5 of 5

Where should roxygen documentation blocks be placed relative to the function they describe?