32 Documentation with roxygen2
R8 provides the roxygen2 roclet r8rd(), which writes a package’s help pages from roxygen2 blocks, with a usage entry for each method. A package lists it in place of rd in DESCRIPTION:
Imports: R8
Config/R8/namespace: resampling
Roxygen: list(markdown = TRUE, roclets = c("collate", "namespace", "R8::r8rd"))
32.1 Types
A type is documented as a function. The usage of a struct is its constructor; required fields have no default:
#' Resamples
#'
#' The training-set indices of each resample.
#'
#' @param resampler `Resampler`: The resampler that drew the resamples.
#' @param n_cases Integer: The number of cases.
#' @param train List of integer vectors: The training cases of each resample.
#'
#' @return A `Resamples` value.
#' @export
Resamples <- R8::new_struct("Resamples", fields = list(...))\usage{
Resamples(resampler, n_cases, train)
}
The usage of an enum lists its variants, with their fields as arguments:
\usage{
\special{Resampler@KFold(n_resamples = NULL, seed = NULL)}
\special{Resampler@Bootstrap(n_resamples = NULL, seed = NULL)}
}
32.2 Generics and methods
A generic is documented as a function. A method’s block holds the @param entries of its own arguments, and the roclet adds the method’s usage to the generic’s help page:
#' @include types.R
NULL
#' Draw resamples
#'
#' @param x A value whose type has a method of `resample()`.
#' @param n_cases Integer: The number of cases.
#' @param ... Arguments of methods.
#'
#' @return A `Resamples` value.
#' @export
resample <- R8::generic("resample", R8::params(x, n_cases, "..."))
#' @param stratify Logical: Ignored by `Bootstrap`.
R8::implement(resample, Resampler) <- function(x, n_cases, ..., stratify = FALSE) {
...
}\usage{
resample(x, n_cases, ...)
## R8 method for enum 'Resampler'
\special{resample(x, n_cases, ..., stratify = FALSE)}
}
In the block of a typed generic, the roclet begins each @param of a typed parameter with the parameter’s type, and writes the return type as the value of a block without @return, so the block states neither:
#' Summarize a value
#'
#' @param x A value whose type has a method of `summarize()`.
#' @param digits The number of digits.
#' @param method The resampling method.
#' @export
summarize <- R8::generic(
"summarize",
R8::params(
x,
digits = R8::arg_integer(min = 0L, default = 3L),
method = R8::arg_option(Resampler)
),
return_type = R8::type_double()
)\arguments{
\item{x}{A value whose type has a method of \code{summarize()}.}
\item{digits}{Integer [0, Inf): The number of digits.}
\item{method}{Optional \code{\link{Resampler}}: The resampling method.}
}
\value{
Double.
}
A type that the package documents is a link. The block of a method with a return type of its own adds it to the generic’s value.
The roclet finds a method without a block by reading the files in R/. A method of a generic that the package does not declare, such as format() or summary(), is listed on the type’s help page; with a method of format() for Resampler, the usage of Resampler is:
\usage{
\special{Resampler@KFold(n_resamples = NULL, seed = NULL)}
\special{Resampler@Bootstrap(n_resamples = NULL, seed = NULL)}
## R8 method for enum 'Resampler'
\special{format(x, ...)}
}
A method of a replacement generic is written as its call, n_resamples(x) <- value, on the generic’s help page, n_resamples-set.Rd. A conversion declared on a converter, implement(as_resampler, "character") <- f, is listed on the converter’s help page as ## R8 method for class 'character', followed by as_resampler(x), or on the target type’s page when the package does not document the converter.
32.3 Three settings of the package
R8::implement(generic, Type) <- function(...)needs no import.@importFrom R8 implement<-imports the replacement function for the form withoutR8::. roxygen2 applies the package’s imports before it loads the code only when aNAMESPACEwritten by roxygen2 exists, so a new package starts with aNAMESPACEholding its header,# Generated by roxygen2: do not edit by hand, before the first run.@include types.Rin the file of methods or impls makes roxygen2 write aCollatefield that loads the type declarations before the methods that name them, since R loads the files ofR/in alphabetical order otherwise.@rdname Resampleon the generic of a Rust-style trait,resample <- Resample@resample, documents the trait and its generic on one help page. Help files namedResample.Rdandresample.Rdcollide on a case-insensitive file system, such as the default file systems of macOS and Windows.