Title: In-Line Documentation for R
Version: 7.3.2
Description: Generate your Rd documentation, 'NAMESPACE' file, and collation field using specially formatted comments. Writing documentation in-line with code makes it easier to keep your documentation up-to-date as your requirements change. 'roxygen2' is inspired by the 'Doxygen' system for C++.
License: MIT + file LICENSE
URL: https://roxygen2.r-lib.org/, https://github.com/r-lib/roxygen2
BugReports: https://github.com/r-lib/roxygen2/issues
Depends: R (≥ 3.6)
Imports: brew, cli (≥ 3.3.0), commonmark, desc (≥ 1.2.0), knitr, methods, pkgload (≥ 1.0.2), purrr (≥ 1.0.0), R6 (≥ 2.1.2), rlang (≥ 1.0.6), stringi, stringr (≥ 1.0.0), utils, withr, xml2
Suggests: covr, R.methodsS3, R.oo, rmarkdown (≥ 2.16), testthat (≥ 3.1.2), yaml
LinkingTo: cpp11
VignetteBuilder: knitr
Config/Needs/development: testthat
Config/Needs/website: tidyverse/tidytemplate
Config/testthat/edition: 3
Config/testthat/parallel: TRUE
Encoding: UTF-8
Language: en-GB
RoxygenNote: 7.3.1.9000
NeedsCompilation: yes
Packaged: 2024-06-27 21:29:24 UTC; hadleywickham
Author: Hadley Wickham ORCID iD [aut, cre, cph], Peter Danenberg [aut, cph], Gábor Csárdi [aut], Manuel Eugster [aut, cph], Posit Software, PBC [cph, fnd]
Maintainer: Hadley Wickham <hadley@posit.co>
Repository: CRAN
Date/Publication: 2024-06-28 08:20:02 UTC

roxygen2: In-Line Documentation for R

Description

logo

Generate your Rd documentation, 'NAMESPACE' file, and collation field using specially formatted comments. Writing documentation in-line with code makes it easier to keep your documentation up-to-date as your requirements change. 'roxygen2' is inspired by the 'Doxygen' system for C++.

Author(s)

Maintainer: Hadley Wickham hadley@posit.co (ORCID) [copyright holder]

Authors:

Other contributors:

See Also

Useful links:


Check markdown escaping

Description

This is a regression test for Markdown escaping.

Usage

double_escape_md(text)

Arguments

text

Input text.

Details

Each of the following bullets should look the same when rendered:

[ this isn't a link ] \[ neither is this \]

Value

Double-escaped text.

Examples

"%" # percent
"\"" # double quote
'\'' # single quote

Escape examples

Description

This documentation topic is used primarily for testing and to record our understanding of the ⁠\example{}⁠ escaping rules. See https://developer.r-project.org/parseRd.pdf for the details provided by R core.

Usage

escape_examples(x)

Examples

# In examples we automatically escape Rd comments (%):
100 %% 30
# even if they are in strings
"50%"

# and \ and \v inside of strings and symbols
"\v" # vertical tab
"\\"
# but not comments: \l \v

# other string escapes are left as is
"\""
"\n"

# Otherwise, backslashes and parentheses are left as is. This
# means that you need to escape unbalanced parentheses, which typically only
# occur in \dontshow{}:

print("Hello")


# You also need to escape backslashes in infix operators and comments
# (this is generally rare)
`%\\%` <- function(x, y) x + y
10 %\% 20
# \\ (renders as two backslashes)

Escape Rd markup, to avoid interpreting it as markdown

Description

This is needed, if we want to stay compatible with existing markup, even if markdown mode is switched on. Fragile Rd tags (tags that may contain markup that can be picked up by the markdown parser), are replaced by placeholders. After the markdown to Rd conversion is done, the original text is put back in place of the placeholders.

It puts back the protected fragile Rd commands into the text after the markdown parsing.

Usage

escape_rd_for_md(text)

unescape_rd_for_md(rd_text, esc_text)

Arguments

text

Input text. Potentially contains Rd and/or markdown markup.

rd_text

The markdown parsed and interpreted text.

esc_text

The original escaped text from escape_rd_for_md().

Details

The list of protected Rd tags is in escaped_for_md.

Some Rd macros are treated specially:

See also roclet-rd.R for the list of tags that uses the markdown-enabled parser. Some tags, e.g. ⁠@aliases⁠, ⁠@backref⁠, etc. only use the standard Roxygen parser.

Value

For escape_rd_for_md: A “safe” version of the input text, where each fragile Rd tag is replaced by a placeholder. The original text is added as an attribute for each placeholder.

For unescape_rd_for_md: Rd text.


Determine if a function is an S3 generic or S3 method.

Description

is_s3_generic compares name to .knownS3Generics and .S3PrimitiveGenerics, then looks at the function body to see if it calls UseMethod().

is_s3_method builds names of all possible generics for that function and then checks if any of them actually is a generic.

Usage

is_s3_generic(name, env = parent.frame())

is_s3_method(name, env = parent.frame())

Arguments

name

Name of function.

env

Base environment in which to look for function definition.


Load package code

Description

roxygen2 is a dynamic documentation system, which means it works with the objects inside your package, not just the source code used to create them. These functions offer various ways of loading your package to suit various constraints:

You can change the default strategy for your function with roxygen2 load option. Override the default off pkgload to use the source or installed strategies:

Roxygen: list(load = "source")

Usage

load_pkgload(path)

load_installed(path)

load_source(path)

Arguments

path

Path to source package


Load roxygen2 options

Description

Options can be stored in the Roxygen field of the DESCRIPTION, or in man/roxygen/meta.R. In either case, the code is parsed and evaluated in a child of the base environment. Call roxy_meta_get() to access current option values from within tag and roclet methods.

Options in man/roxygen/meta.R override those present in DESCRIPTION.

Usage

load_options(base_path = ".")

roxy_meta_get(key = NULL, default = NULL)

Arguments

base_path

Path to package.

Possible options

How to set

Either set in DESCRIPTION:

Roxygen: list(markdown = TRUE, load = "installed")

Or if longer, you can put in ⁠/man/roxygen/meta.R⁠:

list(
  markdown = TRUE,
  load = "installed"
)

Expand the embedded inline code

Description

Expand the embedded inline code

Usage

markdown_pass1(text)

Arguments

text

Input text.

Details

For example this becomes two: 2. Variables can be set and then reused, within the same tag: The value of x is 100.

We have access to the internal functions of the package, e.g. since this is roxygen2, we can refer to the internal markdown function, and this is TRUE: TRUE.

To insert the name of the current package: roxygen2.

The iris data set has 5 columns: Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species.

# Code block demo
x + 1
#> [1] 101

Chunk options:

names(mtcars)
nrow(mtcars)
#>  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
#> [11] "carb"
#> [1] 32

Plots:

plot(1:10)

test-figure-1.png

Alternative knitr engines:

```{r}
# comment
this <- 10
is <- this + 10
good <- this + is

Also see vignette("rd-formatting").

Value

Text with R code expanded. A character vector of the same length as the input text.


Dummy page to test roxygen's markdown formatting

Description

Links are very tricky, so I'll put in some links here: Link to a function: roxygenize(). Link to an object: roxygenize (we just treat it like an object here.

Details

Link to another package, function: desc::desc(). Link to another package, non-function: desc::desc.

Link with link text: this great function, roxygenize, or that great function.

In another package: and this one.

This is a table:

foo bar
1 2
100 200

Roclet: make NAMESPACE

Description

This roclet automates the production of a NAMESPACE file, which controls the functions imported and exported by your package, as described in Writing R extensions.

The NAMESPACE is generated in two passes: the first generates only import directives (because this can be computed without evaluating package code), and the second generates everything (after the package has been loaded).

See vignette("namespace") for details.

Usage

namespace_roclet()

See Also

tags-namespace for tags that generate NAMESPACE directives.

Examples

# The most common namespace tag is @export, which declares that a function
# is part of the external interface of your package
#' @export
foofy <- function(x, y, z) {
}

# You'll also often find global imports living in a file called
# R/{package}-package.R.
#' @importFrom magrittr %>%
#' @import rlang
NULL

Constructors for S3 object to represent R objects.

Description

These objects are usually created by the parsers, but it is also useful to generate them by hand for testing.

Usage

object(value, alias, type)

Arguments

value

The object itself.

alias

Alias for object being documented, in case you create a generator function with different name.


Default format for data

Description

This function is called to generate the default "Format" section for each data object. The default implementation will return the class and dimension information.

Usage

object_format(x)

Arguments

x

A data object

Value

A character value with valid Rd syntax, or NULL.


Parse a package, file, or inline code

Description

parse_package(), parse_file(), and parse_text() allow you to use roxygen's parsing code to parse the roxygen blocks from a package, file, or character vector of code. env_package() and env_file() provide defaults that generate a temporary environment making it possible to associate each block with the corresponding live object.

Usage

parse_package(path = ".", env = env_package(path))

parse_file(file, env = env_file(file), srcref_path = NULL)

parse_text(text, env = env_file(file))

env_file(file)

env_package(path)

Arguments

path, file, text

Either specify a path to the root directory of a package, an R file, or a character vector text.

env

An environment environment containing the result of evaluating the input code. The defaults will do this for you in a test environment: for real code you'll need to generate the environment yourself.

You can also set to NULL if you only want to get the tokenized code blocks only. This suppresses evaluation of ⁠@eval⁠ tags, and will not find the code object associated with each block.

Value

A list of roxy_block objects


Roclet: make Rd files

Description

This roclet is the workhorse of roxygen2, producing the .Rd files that R uses to document functions, datasets, packages, classes, and more. See vignette("rd") for details.

Generally you will not call this function directly but will instead use roxygenise() specifying the rd roclet.

Usage

rd_roclet()

See Also

tags-rd, tags-rd-other, tags-reuse, tags-index-crossref for tags provided by this roclet.

Examples

#' The length of a string (in characters)
#'
#' @param x A character vector.
#' @returns An integer vector the same length as `x`.
#'   `NA` strings have `NA` length.
#' @seealso [nchar()]
#' @export
#' @examples
#' str_length(letters)
#' str_length(c("i", "like", "programming", NA))
str_length <- function(x) {
}

Construct an rd_section object

Description

An rd_section represents an Rd command that can appear at the top-level of an Rd document, like ⁠\name{}⁠, ⁠\title{}⁠, ⁠\description{}⁠, or ⁠\section{}⁠.

Usage

rd_section(type, value)

Arguments

type

Section type. Stored in type field, and in class ⁠rd_section_{type}⁠. To avoid namespace clashes between different extensions, this should include the package name.

value

Section data. Only used by format() and merge() methods.

Methods

If provide your own rd_section type, you'll also need to define a ⁠format.rd_section_{type}⁠ method that returns formatted Rd output. You may also need to provide a ⁠merge.rd_section_{type}⁠ method if two sections can not be combined with rd_section(x$type, c(x$value, y$value)). See vignette("extending") for more details.


Process roclet on string and capture results.

Description

Useful for testing.

Usage

roc_proc_text(roclet, input, wd = NULL)

Arguments

roclet

Name of roclet to use for processing.

input

Source string

wd

Working directory


Build a new roclet.

Description

To create a new roclet, you will need to create a constructor function that wraps roclet, and then implement the methods described below.

Usage

roclet(subclass, ...)

roclet_preprocess(x, blocks, base_path)

roclet_process(x, blocks, env, base_path)

roclet_output(x, results, base_path, ...)

roclet_clean(x, base_path)

roclet_tags(x)

Arguments

x

A roclet object.

blocks

A list of roxy_block objects.

base_path

Path to root of source package.

env

Package environment.

results

Value returned from your roclet_process() method.

Methods

Deprecated methods

roclet_tags() is no longer used; instead provide a roxy_tag_parse() method for each tag.


Create a roclet from a string.

Description

This provides a flexible way of specifying a roclet in a string.

Usage

roclet_find(x)

Arguments

x

Arbitrary R code evaluated in roxygen2 package.

Examples

# rd, namespace, and vignette work for backward compatibility
roclet_find("rd")

# But generally you should specify the name of a function that
# returns a roclet
roclet_find("rd_roclet")

# If it lives in another package, you'll need to use ::
roclet_find("roxygen2::rd_roclet")

# If it takes parameters (which no roclet does currently), you'll need
# to call the function
roclet_find("roxygen2::rd_roclet()")

Blocks

Description

A roxy_block represents a single roxygen2 block.

The ⁠block_*⁠ functions provide a few helpers for common operations:

Usage

roxy_block(tags, file, line, call, object = NULL)

block_has_tags(block, tags)

block_get_tags(block, tags)

block_get_tag(block, tag)

block_get_tag_value(block, tag)

Arguments

tags

A list of roxy_tags.

file, line

Location of the call (i.e. the line after the last line of the block).

call

Expression associated with block.

object

Optionally, the object associated with the block, found by inspecting/evaluating call.

block

A roxy_block to manipulate.

tag

A single tag name.

Examples

# The easiest way to see the structure of a roxy_block is to create one
# using parse_text:
text <- "
  #' This is a title
  #'
  #' @param x,y A number
  #' @export
  f <- function(x, y) x + y
"

# parse_text() returns a list of blocks, so I extract the first
block <- parse_text(text)[[1]]
block

roxy_tag S3 constructor

Description

roxy_tag() is the constructor for tag objects. roxy_tag_warning() is superseded by warn_roxy_tag(); use to generate a warning that includes the location of the tag.

Usage

roxy_tag(tag, raw, val = NULL, file = NA_character_, line = NA_character_)

roxy_tag_parse(x)

roxy_tag_warning(x, ...)

warn_roxy_tag(tag, message, parent = NULL, envir = parent.frame())

Arguments

tag

Tag name. Arguments starting with . are reserved for internal usage.

raw

Raw tag value, a string.

val

Parsed tag value, typically a character vector, but sometimes a list. Usually filled in by tag_parsers

file, line

Location of the tag

x

A tag

Methods

Define a method for roxy_tag_parse to support new tags. See tag_parsers for more details.


Generate Rd output from a tag

Description

Provide a method for this generic if you want a tag to generate output in .Rd files. See vignette("extending") for more details.

Usage

roxy_tag_rd(x, base_path, env)

Arguments

x

The tag

base_path

Path to package root directory.

env

Environment in which to evaluate code (if needed)

Value

Methods must return a rd_section.


Process a package with the Rd, namespace and collate roclets

Description

This is the workhorse function that uses roclets, the built-in document transformation functions, to build all documentation for a package. See the documentation for the individual roclets, rd_roclet(), namespace_roclet(), and for update_collate(), for more details.

Usage

roxygenize(package.dir = ".", roclets = NULL, load_code = NULL, clean = FALSE)

roxygenise(package.dir = ".", roclets = NULL, load_code = NULL, clean = FALSE)

Arguments

package.dir

Location of package top level directory. Default is working directory.

roclets

Character vector of roclet names to use with package. The default, NULL, uses the roxygen roclets option, which defaults to c("collate", "namespace", "rd").

load_code

A function used to load all the R code in the package directory. The default, NULL, uses the strategy defined by the load roxygen option, which defaults to load_pkgload(). See load for more details.

clean

If TRUE, roxygen will delete all files previously created by roxygen before running each roclet.

Details

Note that roxygen2 is a dynamic documentation system: it works by inspecting loaded objects in the package. This means that you must be able to load the package in order to document it: see load for details.

Value

NULL


A RoxyTopic is an ordered collection of unique rd_sections

Description

A RoxyTopic object corresponds to a generated .Rd file.

Public fields

sections

Named list of sections. Each item must be an rd_section() object.

filename

Path to the .Rd file to generate.

Methods

Public methods


Method format()

Format the .Rd file. It considers the sections in particular order, even though Rd tools will reorder them again.

Usage
RoxyTopic$format(...)
Arguments
...

Passed to the format() methods of the rd_section() objects, the sections.

Returns

Character string.


Method is_valid()

Check if an .Rd file is valid

Usage
RoxyTopic$is_valid()
Returns

Logical flag, TRUE for valid .Rd files


Method has_section()

Check if an .Rd file has a certain section.

Usage
RoxyTopic$has_section(type)
Arguments
type

Section type, a character scalar.

Returns

Logical flag.


Method get_section()

Query a section.

Usage
RoxyTopic$get_section(type)
Arguments
type

Section type, a character scalar.

Returns

The rd_section object representing the section, or NULL if the topic has no such section.


Method get_value()

Query the value of a section. This is the value of the rd_section object.

Usage
RoxyTopic$get_value(type)
Arguments
type

Section type, a character scalar.

Returns

Value.


Method get_rd()

Get the Rd code of a section.

Usage
RoxyTopic$get_rd(type)
Arguments
type

Section type, a character scalar.

Returns

Character vector, one element per line.


Method get_name()

Get the value of the name section. This is the name of the Rd topic.

Usage
RoxyTopic$get_name()
Returns

Character scalar.


Method inherits_from()

Query the topics this topic inherits type from.

Usage
RoxyTopic$inherits_from(type)
Arguments
type

Section type, a character scalar.

Returns

A character vector of topic names.


Method inherits_section_from()

Query the topics this topic inherits sections from.

Usage
RoxyTopic$inherits_section_from()
Returns

A character vector of topic names.


Method add()

Add one or more sections to the topic.

Usage
RoxyTopic$add(x, block = "???", overwrite = FALSE)
Arguments
x

Section(s) to add. It may be another RoxyTopic object, all of its sections will be added; or an rd_section object; or a list of rd_section objects to add.

block

Name of block to use in error messages.

overwrite

Whether to overwrite an existing section. If FALSE then the two sections will be merged.


Method add_section()

Add a section.

Usage
RoxyTopic$add_section(section, block = "???", overwrite = FALSE)
Arguments
section

rd_section object to add.

block

Name of block to use in error messages.

overwrite

Whether to overwrite an existing section. If FALSE then the two sections will be merged.

Details

Ensures that each type of name (as given by its name), only appears once in self$sections. This method if for internal use only.


Method clone()

The objects of this class are cloneable with this method.

Usage
RoxyTopic$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.


Parse tags

Description

These functions parse the raw tag value, convert a string into a richer R object and storing it in val, or provide an informative warning and returning NULL.

Usage

tag_value(x)

tag_inherit(x)

tag_name(x)

tag_two_part(x, first, second, required = TRUE, markdown = TRUE)

tag_name_description(x)

tag_words(x, min = 0, max = Inf)

tag_words_line(x)

tag_toggle(x)

tag_code(x)

tag_examples(x)

tag_markdown(x)

tag_markdown_with_sections(x)

Arguments

x

A roxy_tag object to parse

first, second

Name of first and second parts of two part tags

required

Is the second part required (TRUE) or can it be blank (FALSE)?

markdown

Should the second part be parsed as markdown?

min, max

Minimum and maximum number of words

Value

A roxy_tag object with the val field set to the parsed value.

New tag

To create a new ⁠@mytag⁠ define roxy_tag_parse.roxy_tag_mytag(). It should either call one of the functions here, or directly set x$val.


Access metadata about built-in tags

Description

Access metadata about built-in tags

Usage

tags_list(built_in = TRUE)

tags_metadata()

Tags for indexing and cross-references

Description

Learn the full details in vignette('index-crossref').

Key tags:

Other less frequently used tags:

Usage

#' @aliases ${1:alias}
#' @backref ${1:path}
#' @concept ${1:concept}
#' @family ${1:family name}
#' @keywords ${1:keyword}
#' @references ${1:reference}
#' @seealso [${1:func}()]

See Also

Other documentation tags: tags-rd, tags-rd-other, tags-reuse


Tags for managing the NAMESPACE

Description

Learn the full details in vignette('namespace').

Key tags:

Other less frequently used tags:

Usage

#' @evalNamespace ${1:r-code}
#' @export
#' @exportClass ${1:class}
#' @exportMethod ${1:generic}
#' @exportPattern ${1:pattern}
#' @exportS3Method ${1:package}::${2:generic}
#' @import ${1:package}
#' @importClassesFrom ${1:package} ${2:class}
#' @importFrom ${1:package} ${2:function}
#' @importMethodsFrom ${1:package} ${2:generic}
#' @rawNamespace ${1:namespace directives}
#' @useDynLib ${1:package}

Tags for documenting functions

Description

Learn the full details in vignette('rd').

Key tags:

Other less frequently used tags:

Usage

#' @description${1:A short description...}

#' @details${1:Additional details...}

#' @example ${1:path}.R
#' @examples${1:# example code}

#' @examplesIf ${1:condition}${2:# example code}

#' @noRd
#' @param ${1:name} ${2:description}
#' @rawRd ${1:rd}
#' @return ${1:description}
#' @returns ${1:description}
#' @title ${1:title}
#' @usage ${1:fun}(${2:arg1, arg2 = default, ...})

See Also

Other documentation tags: tags-index-crossref, tags-rd-other, tags-reuse


Tags related to markdown support

Description

Learn the full details in vignette('rd-formatting').

Other less frequently used tags:

Usage

#' @md
#' @noMd
#' @section ${1:section title}:

Tags for documenting datasets and classes

Description

Learn the full details in vignette('rd-other').

Key tags:

Usage

#' @field ${1:name} ${2:description}
#' @format ${1:description}
#' @method ${1:generic} ${2:class}
#' @slot ${1:name} ${2:description}
#' @source ${1:description}

See Also

Other documentation tags: tags-index-crossref, tags-rd, tags-reuse


Tags that help you reuse documentation

Description

Learn the full details in vignette('reuse').

Key tags:

Other less frequently used tags:

Usage

#' @describeIn ${1:destination} ${2:description}
#' @eval ${1:r-code}
#' @evalRd ${1:r-code}
#' @includeRmd man/rmd/${1:filename}.Rmd
#' @inherit ${1:source} ${2:components}
#' @inheritDotParams ${1:source} ${2:arg1 arg2 arg3}
#' @inheritParams ${1:source}
#' @inheritSection ${1:source} ${2:section name}
#' @order ${1:number}
#' @rdname ${1:topic-name}
#' @template ${1:path-to-template}
#' @templateVar ${1:name} ${2:value}

See Also

Other documentation tags: tags-index-crossref, tags-rd, tags-rd-other


Update Collate field in DESCRIPTION

Description

By default, R loads files in alphabetical order. Unfortunately not every alphabet puts letters in the same order, so you can't rely on alphabetic ordering if you need one file loaded before another. (This usually doesn't matter but is important for S4, where you need to make sure that classes are loaded before subclasses and generics are defined before methods.). You can override the default alphabetical ordering with ⁠@include before.R⁠, which specify that before.R must be loaded before the current file.

Generally, you will not need to run this function yourself; it should be run automatically by any package that needs to load your R files in collation order.

Usage

update_collate(base_path)

Arguments

base_path

Path to package directory.

Collate

This is not a roclet because roclets need the values of objects in a package, and those values can not be generated unless you've sourced the files, and you can't source the files unless you know the correct order.

If there are no ⁠@include⁠ tags, roxygen2 will leave collate as is. This makes it easier to use roxygen2 with an existing collate directive, but if you remove all your ⁠@include⁠ tags, you'll need to also manually delete the collate field.

Examples

#' If `example-a.R', `example-b.R' and `example-c.R' live in R/
#' and we're in `example-a.R`, then the following @include statement
#' ensures that example-b and example-c are sourced before example-a.
#' @include example-b.R example-c.R
NULL

Re-build outdated vignettes

Description

This roclet rebuilds outdated vignettes with tools::buildVignette, but we no longer recommend it because we no longer recommend storing built vignettes in a package.

By default, it will rebuild all vignettes if the source file is newer than the output pdf or html. (This means it will automatically re-build the vignette if you change the vignette source, but not when you change the R code). If you want finer control, add a Makefile to ⁠vignettes/⁠ and roxygen2 will use that instead.

To prevent RStudio from re-building the vignettes again when checking your package, add --no-build-vignettes to the "Build Source Package" field in your project options.

Usage

vignette_roclet()