This vignette describes the most important roxygen2 tags for
documenting functions. See vignette("reuse") for reusing
documentation across topics.
For a user to use a function in your package, you must
export it with @export. If you export a
function, you must also document it, and since other people will use it,
you need to be careful if you later change the function interface. We
usually put @export in between @returns and
@examples:
#' Add two numbers together
#'
#' @param x,y A pair of numbers.
#' @returns A number.
#' @export
#' @examples
#' 1 + 2
add <- function(x, y) {
x + y
}You’ll learn what all these are pieces are next!
Each documentation block starts with some text which defines the
title, the description, and the details. Here’s an example showing what
the documentation for sum() might look like if it had been
written with roxygen:
#' Sum of vector elements
#'
#' `sum` returns the sum of all the values present in its arguments.
#'
#' This is a generic function: methods can be defined for it directly
#' or via the [Summary()] group generic. For this to work properly,
#' the arguments `...` should be unnamed, and dispatch is on the
#' first argument.
sum <- function(..., na.rm = TRUE) {}This introductory block is broken up as follows:
The first sentence is the title: that’s what you
see when you look at help(package = mypackage) and is shown
at the top of each help file. It should generally fit on one line, be
written in sentence case, and not end in a full stop.
The second paragraph is the description: this comes first in the documentation and should briefly describe what the function does.
The third and subsequent paragraphs go into the details: this is a (often long) section that comes after the argument description and should provide any other important details of how the function operates. The details are optional.
The title appears in package indexes and search results, so think about how users will find your function. Use synonyms to describe what the function does. For example, dplyr uses titles like:
filter())mutate())summarise())The description should briefly summarize the goal of the function, usually in one paragraph. This can be challenging for simple functions, because it can feel like you’re just repeating the title of the function. Try to find a slightly different wording, if you can. It’s okay if this feels a little repetitive; it’s often useful for users to see the same thing expressed in two different ways. It’s a little extra work, but the extra effort is often worth it.
#' Detect the presence/absence of a match
#'
#' `str_detect()` returns a logical vector with `TRUE` for each element of
#' `string` that matches `pattern` and `FALSE` otherwise. It's equivalent to
#' `grepl(pattern, string)`.If you need a multi-paragraph description, use an explicit
@description tag:
#' View strings and matches
#'
#' @description
#' `str_view()` is used to print the underlying representation of a string and
#' to see how a `pattern` matches.
#'
#' Matches are surrounded by `<>` and unusual whitespace (i.e. all whitespace
#' apart from `" "` and `"\n"`) are surrounded by `{}` and escaped.Basically, if you’re going to include an empty line in your
description, you’ll need to use an explicit @description
tag.
The details section is optional and comes after the argument description in the rendered help. Use informative markdown headings to break up long details.
#' Sum of vector elements
#'
#' @description
#' `sum` returns the sum of all the values present in its arguments.
#'
#' @details
#' This is a generic function: methods can be defined for it directly
#' or via the [Summary()] group generic. For this to work properly,
#' the arguments `...` should be unnamed, and dispatch is on the
#' first argument.