roxygen2 provides several ways to avoid repeating yourself in code documentation, while assembling information from multiple places in one documentation file:
Combine documentation for closely related functions into a single
file with @describeIn or @rdname.
Automatically copy tags with @inheritParams,
@inheritSection, or @inherit.
Use functions to generate repeated text with inline R code.
Share text between documentation and vignettes with child documents.
The chapter concludes by showing you how to update superseded reuse
mechanisms that we no longer recommend: @includeRmd,
@eval/@evalRd, and @template.
You can document multiple functions in the same file by using either
@rdname or @describeIn tag. It’s a technique
best used with care: documenting too many functions in one place leads
to confusion. Use it when all functions have the same (or very similar)
arguments.
@rdnameUse @rdname <destination>1 to include multiple
functions in the same page. Tags (e.g. @title,
@description, @examples) will be combined,
across blocks but often this yields text that is hard to understand. So
we recommend that you make one block that contains the title,
description, common parameters, examples and so on, and then only
document individual parameters in the other blocks.
There are two basic ways to do that. You can create a standalone
documentation block and then add the functions to it. This typically
works best when you have a family of related functions and you want to
link to that family from other functions (i.e. trig in the
examples below).
#' Trigonometric approximations
#' @param x Input, in radians.
#' @name trig
NULL
#> NULL
#' @rdname trig
#' @export
sin_ish <- function(x) x - x^3 / 6
#' @rdname trig
#' @export
cos_ish <- function(x) 1 - x^2 / 2
#' @rdname trig
#' @export
tan_ish <- function(x) x + x^3 / 3Alternatively, you can add docs to an existing function. This tends to work better if you have a “primary” function with some variants or some helpers.
@describeInAn alternative to @rdname is @describeIn.
It has a slightly different syntax because as well as a topic name, you
also provide a brief description for the function, like
@describein topic one sentence description. The primary
difference between @rdname and @describeIn, is
that @describeIn creates a new section containing a
bulleted list of each function, along with its description. It uses a
number of heuristics to determine the heading of this section, depending
on if you’re documenting related functions, methods for a generic, or
methods for a class.
In general, I no longer recommend @describeIn because I
don’t think the heuristics it uses are as good as a thoughtful
hand-crafted summary. If you’re currently using
@describeIn, you can generally replace it with
@rdname, as long as you give some thought to the
multiple-function @description.
By default, roxygen blocks are processed in the order in which they
appear in the file. When you’re combining multiple files, this can
sometimes cause the function usage to appear in a suboptimal order. You
can override the default ordering with @order. For example,
the following the block would place times first in
arith.Rd because 1 comes before 2.
To insert code inline, enclose it in `r `. Roxygen will
interpret the rest of the text within backticks as R code and evaluate
it, and replace the backtick expression with its value. Here’s a simple
example:
This is equivalent to writing:
The resulting text, together with the whole tag is interpreted as markdown, as usual. This means that you can use R to dynamically write markdown. For example if you defined this function in your package:
You could then write:
The result is equivalent to writing the following by hand:
This is a powerful technique for reducing duplication because you can flexibly parameterise the function however best meets your needs. Note that the evaluation environment is deliberately a child of the package that you’re documenting so you can call internal functions.
You can use `Rd ` to run code that is evaluated when a
user views the help page, rather than when roxygen2 builds the
documentation. This is useful for documentation that needs to be
dynamic, reflecting the current state of R, of the user’s library and
system (e.g. what dependencies are installed), not the state when the
package was built. It works by using the \Sexpr tag, so
that `Rd myFun()`
generates\Sexpr[stage=render,results=rd]{myFun()}.
Here’s a simple example, which will greet the user appropriately, depending on the time of day:
greeting <- function() {
hour <- as.POSIXlt(Sys.time(), tz = "UTC")$hour
if (hour < 12) {
"Good morning!"
} else if (hour < 18) {
"Good afternoon!"
} else {
"Good evening!"
}
}
#' Title
#'
#' `Rd roxygen2:::greeting()`
foo <- function() NULLNote that compared to inline R code, the function is run in the
global environment by R’s documentation system, not inside the package
namespace by roxygen2. This generally means that you’ll want to use
::: to explicitly call an internal function.
You can use the same .Rmd or .md document
in the documentation, README.Rmd, and vignettes by using
child documents:
```{r child = "common.Rmd"}
```
The included Rmd file can have roxygen Markdown-style links to other
help topics. E.g. [roxygen2::roxygenize()] will link to the
manual page of the roxygenize function in roxygen2. See
vignette("rd-formatting") for details.
If the Rmd file contains roxygen (Markdown-style) links to other help topics, then some care is needed, as those links will not work in Rmd files by default. A workaround is to specify external HTML links for them. These external locations will not be used for the manual which instead always links to the help topics in the manual. Example:
See also the [roxygen2::roxygenize()] function.
[roxygen2::roxygenize()]: https://roxygen2.r-lib.org/reference/roxygenize.html
This example will link to the supplied URLs in HTML / Markdown files
and it will link to the roxygenize help topic in the
manual.
Note that if you add external link targets like these, then roxygen will emit a warning about these link references being defined multiple times (once externally, and once to the help topic). This warning originates in Pandoc, and it is harmless.
Over the years, we have experimented with a number of other ways to reduce duplication across documentation files. A number of these are now superseded and we recommend changing them to use the techniques described above:
Instead of @includeRmd man/rmd/example.Rmd, use a
child document.
Instead of @eval or @evalRd, use inline
R code.
Instead of @template and @templateVars
write your own function and call it from inline R code.
Inline R markdown can only generate markdown text within a tag so in
principle it is less flexible than
@eval/@evalRd/@template. However,
our experience has revealed that generating multiple tags at once tends
to be rather inflexible, and you often end up refactoring into smaller
pieces so we don’t believe this reflects a real loss of
functionality.
The destination is a topic name. There’s a one-to-one
correspondence between topic names and .Rd files where a
topic called foo will produce a file called
man/foo.Rd.↩︎
Need a high-speed mirror for your open-source project?
Contact our mirror admin team at info@clientvps.com.
This archive is provided as a free public service to the community.
Proudly supported by infrastructure from VPSPulse , RxServers , BuyNumber , UnitVPS , OffshoreName and secure payment technology by ArionPay.