From 701871972763226925a5fce52909b63103b8fc07 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Fri, 8 Apr 2022 11:14:57 -0500 Subject: [PATCH] Reformat vignette with visual editor --- vignettes/rd.Rmd | 470 ++++++++++++++++++++++++----------------------- 1 file changed, 239 insertions(+), 231 deletions(-) diff --git a/vignettes/rd.Rmd b/vignettes/rd.Rmd index 34b9b4f2..a1adb239 100644 --- a/vignettes/rd.Rmd +++ b/vignettes/rd.Rmd @@ -13,14 +13,21 @@ knitr::opts_chunk$set(comment = "#>", collapse = TRUE) ## Basics -A roxygen __block__ is a sequence of lines starting with `#'` (optionally preceded by whitespace). -The first lines of the block forms the __description__, described next. The description continues until the first __tag__. Tags start with `@`, like `@details` or `@param` , and must appear at the beginning of a line. The content of a tag extends to the start of the next tag or the end of the block. Text within the description or tags can be formatted using Markdown or `Rd` commands; see `vignette("rd-formatting")` for details. +A roxygen **block** is a sequence of lines starting with `#'` (optionally preceded by whitespace). +The first lines of the block forms the **description**, described next. +The description continues until the first **tag**. +Tags start with `@`, like `@details` or `@param` , and must appear at the beginning of a line. +The content of a tag extends to the start of the next tag or the end of the block. +Text within the description or tags can be formatted using Markdown or `Rd` commands; see `vignette("rd-formatting")` for details. -A block continues until it hits R code, typically a function or object assignment. Blocks ignore empty lines, including lines made up of non-roxygen comments. If you need to separate two blocks, use `NULL`. +A block continues until it hits R code, typically a function or object assignment. +Blocks ignore empty lines, including lines made up of non-roxygen comments. +If you need to separate two blocks, use `NULL`. ## The description block -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: +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: ```{r} #' Sum of vector elements @@ -36,20 +43,16 @@ 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. - -You can also use explicit `@title`, `@description`, and `@details` tags. This is unnecessary unless you want to have a multi-paragraph description, bulleted list, or other more exotic structure. +- 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. + +You can also use explicit `@title`, `@description`, and `@details` tags. +This is unnecessary unless you want to have a multi-paragraph description, bulleted list, or other more exotic structure. ```{r} #' Sum of vector elements @@ -66,75 +69,64 @@ You can also use explicit `@title`, `@description`, and `@details` tags. This is ## Object specifics -Further details of roxygen2 depend on what you're documenting. The following sections describe the most commonly used tags for functions, S3, S4, datasets, and packages. +Further details of roxygen2 depend on what you're documenting. +The following sections describe the most commonly used tags for functions, S3, S4, datasets, and packages. ### Functions -Functions are the mostly commonly documented objects. Most functions use three tags: - -* `@param name description` describes the inputs to the function. - The description should provide a succinct summary of parameter type - (e.g. a string, a numeric vector), and if not obvious from - the name, what the parameter does. The description should start with a - capital letter and end with a full stop. It can span multiple lines (or - even paragraphs) if necessary. All parameters must be documented. - - You can document multiple arguments in one place by separating - the names with commas (no spaces). For example, to document both - `x` and `y`, you can say `@param x,y Numeric vectors`. - -* `@examples` provides executable R code showing how to use the function in - practice. This is a very important part of the documentation because - many people look at the examples before reading anything. Example code - must work without errors as it is run automatically as part of `R CMD - check`. - - However, for the purpose of illustration, it's often useful to include code - that causes an error. `\dontrun{}` allows you to include code in the - example that is never used. There are two other special commands. - `\dontshow{}` is run, but not shown in the help page: this can - be useful for informal tests. `\donttest{}` is run in examples, - but not run automatically in `R CMD check`. This is useful if you - have examples that take a long time to run. The options are summarised - below. - - Command | example | help | R CMD check | R CMD check --as-cran - ------------ | --------- | ------ | ----------- | --------------------- - `\dontrun{}` | | x | | - `\dontshow{}`| x | | x | x - `\donttest{}`| x | x | | x - - Note that `R CMD check --as-cran` is run for incoming CRAN checks but - not for regular CRAN checks. - +Functions are the mostly commonly documented objects. +Most functions use three tags: + +- `@param name description` describes the inputs to the function. + The description should provide a succinct summary of parameter type (e.g. a string, a numeric vector), and if not obvious from the name, what the parameter does. + The description should start with a capital letter and end with a full stop. + It can span multiple lines (or even paragraphs) if necessary. + All parameters must be documented. + + You can document multiple arguments in one place by separating the names with commas (no spaces). + For example, to document both `x` and `y`, you can say `@param x,y Numeric vectors`. + +- `@examples` provides executable R code showing how to use the function in practice. + This is a very important part of the documentation because many people look at the examples before reading anything. + Example code must work without errors as it is run automatically as part of `R CMD check`. + + However, for the purpose of illustration, it's often useful to include code that causes an error. + `\dontrun{}` allows you to include code in the example that is never used. + There are two other special commands. + `\dontshow{}` is run, but not shown in the help page: this can be useful for informal tests. + `\donttest{}` is run in examples, but not run automatically in `R CMD check`. + This is useful if you have examples that take a long time to run. + The options are summarised below. + + | Command | example | help | R CMD check | R CMD check --as-cran | + |---------------|---------|------|-------------|-----------------------| + | `\dontrun{}` | | x | | | + | `\dontshow{}` | x | | x | x | + | `\donttest{}` | x | x | | x | + + Note that `R CMD check --as-cran` is run for incoming CRAN checks but not for regular CRAN checks. + For finer control, you can use `@examplesIf` - - ```r + + ``` r #' @examplesIf interactive() #' browseURL("https://roxygen2.r-lib.org") ``` - + will generate - - ``` - \examples{ - \dontshow{if (interactive() (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} - gh_organizations(since = 42) - \dontshow{\}) # examplesIf} - } - ``` - - This way, the code evaluating whether the example can be run is not - shown to users reading the help, but it still prevents R CMD check failures. - - Instead of including examples directly in the documentation, you can - put them in separate files and use `@example path/relative/to/package/root` - to insert them into the documentation. - -* `@return description` describes the output from the function. This is - not always necessary, but is a good idea if you return different types - of outputs depending on the input, or you're returning an S3, S4 or RC - object. + + \examples{ + \dontshow{if (interactive() (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} + gh_organizations(since = 42) + \dontshow{\}) # examplesIf} + } + + This way, the code evaluating whether the example can be run is not shown to users reading the help, but it still prevents R CMD check failures. + + Instead of including examples directly in the documentation, you can put them in separate files and use `@example path/relative/to/package/root` to insert them into the documentation. + +- `@return description` describes the output from the function. + This is not always necessary, but is a good idea if you return different types of outputs depending on the input, or you're returning an S3, S4 or RC object. We could use these new tags to improve our documentation of `sum()` as follows: @@ -173,38 +165,35 @@ We could use these new tags to improve our documentation of `sum()` as follows: sum <- function(..., na.rm = TRUE) {} ``` -Indent the second and subsequent lines of a tag so that when scanning the documentation so it's easy to see where one tag ends and the next begins. Tags that always span multiple lines (like `@example`) should start on a new line and don't need to be indented. +Indent the second and subsequent lines of a tag so that when scanning the documentation so it's easy to see where one tag ends and the next begins. +Tags that always span multiple lines (like `@example`) should start on a new line and don't need to be indented. ### S3 -* S3 __generics__ are regular functions, so document them as such. - If necessary, include a `@section` that provides additional details for - method implementors - -* S3 __classes__ have no formal definition, so document the - [constructor](https://adv-r.hadley.nz/s3.html#s3-constructor). - -* It is your choice whether or not to document S3 __methods__. Generally, it's - not necessary to document straightforward methods for common generics like - `print()`. (You should, however, always `@export` S3 methods). - - If your method is more complicated, you should document it by setting - `@rdname`. Typically you will document methods with their generic, so you'd - document `foofy.data.frame` by setting `@rdname`. In base R, you can find - documentation for more complex methods like `?predict.lm`, `?predict.glm`, - and `?anova.glm`. - - Generally, roxygen2 will automatically figure out the generic that the method - belongs to, and you should only need to use `@method` if there is ambiguity. - For example, is `all.equal.data.frame()` the `equal.data.frame` method for - `all()`, or the `data.frame` method for `all.equal()`?. If this happens to - you, disambiguate with (e.g.) `@method all.equal data.frame`. +- S3 **generics** are regular functions, so document them as such. + If necessary, include a `@section` that provides additional details for method implementors + +- S3 **classes** have no formal definition, so document the [constructor](https://adv-r.hadley.nz/s3.html#s3-constructor). + +- It is your choice whether or not to document S3 **methods**. + Generally, it's not necessary to document straightforward methods for common generics like `print()`. + (You should, however, always `@export` S3 methods). + + If your method is more complicated, you should document it by setting `@rdname`. + Typically you will document methods with their generic, so you'd document `foofy.data.frame` by setting `@rdname`. + In base R, you can find documentation for more complex methods like `?predict.lm`, `?predict.glm`, and `?anova.glm`. + + Generally, roxygen2 will automatically figure out the generic that the method belongs to, and you should only need to use `@method` if there is ambiguity. + For example, is `all.equal.data.frame()` the `equal.data.frame` method for `all()`, or the `data.frame` method for `all.equal()`?. + If this happens to you, disambiguate with (e.g.) `@method all.equal data.frame`. ### S4 -S4 __generics__ are also functions, so document them as such. +S4 **generics** are also functions, so document them as such. -Document __S4 classes__ by adding a roxygen block before `setClass()`. Use `@slot` to document the slots of the class. Here's a simple example: +Document **S4 classes** by adding a roxygen block before `setClass()`. +Use `@slot` to document the slots of the class. +Here's a simple example: ```{r} #' An S4 class to represent a bank account @@ -215,56 +204,49 @@ Account <- setClass("Account", ) ``` -S4 __methods__ are a little more complicated. Unlike S3 methods, all S4 methods must be documented. You can document them in three places: +S4 **methods** are a little more complicated. +Unlike S3 methods, all S4 methods must be documented. +You can document them in three places: -* In the class. Most appropriate if the corresponding generic uses single - dispatch and you created the class. +- In the class. + Most appropriate if the corresponding generic uses single dispatch and you created the class. -* In the generic. Most appropriate if the generic uses multiple dispatches - and you control it. +- In the generic. + Most appropriate if the generic uses multiple dispatches and you control it. -* In its own file. Most appropriate if the method is complex. or the - either two options don't apply. +- In its own file. + Most appropriate if the method is complex. + or the either two options don't apply. -Use either `@rdname` or `@describeIn` to control where method documentation goes. See the next section for more details. +Use either `@rdname` or `@describeIn` to control where method documentation goes. +See the next section for more details. ### R6 -Starting from version 7.0.0 roxygen treats documentation for R6 classes -specially: +Starting from version 7.0.0 roxygen treats documentation for R6 classes specially: -* R6 methods can be documented in-line, i.e. the method's documentation - comments come right before the definition of the method. +- R6 methods can be documented in-line, i.e. the method's documentation comments come right before the definition of the method. -* Method documentation can use the `@description`, `@details`, - `@param`, `@return` and `@examples` tags. These are used to create a - subsection for the method, within a separate 'Methods' section. - All roxygen comment lines of a method documentation must appear after - a tag. +- Method documentation can use the `@description`, `@details`, `@param`, `@return` and `@examples` tags. + These are used to create a subsection for the method, within a separate 'Methods' section. + All roxygen comment lines of a method documentation must appear after a tag. -* `@param` tags that appear before the class definition are automatically - inherited by all methods, if needed. +- `@param` tags that appear before the class definition are automatically inherited by all methods, if needed. -* R6 fields and active bindings can make use of the `@field` tag. - Their documentation should also be in-line. +- R6 fields and active bindings can make use of the `@field` tag. + Their documentation should also be in-line. -* Roxygen2 checks that all public methods, public fields, active - bindings and all method arguments are documented, and issues warnings - otherwise. +- Roxygen2 checks that all public methods, public fields, active bindings and all method arguments are documented, and issues warnings otherwise. -* To turn off the special handling of R6 classes and go back to the - roxygen2 6.x.x behavior, use the `r6 = FALSE` option in `DESCRIPTION`, - in the `Roxygen` entry: `Roxygen: list(r6 = FALSE)`. +- To turn off the special handling of R6 classes and go back to the roxygen2 6.x.x behavior, use the `r6 = FALSE` option in `DESCRIPTION`, in the `Roxygen` entry: `Roxygen: list(r6 = FALSE)`. Roxygen2 automatically generates additional sections for an R6 class: -* A section with information about the superclass(es) of the class, - with links. In HTML this includes a list of all inherited methods, - with links. +- A section with information about the superclass(es) of the class, with links. + In HTML this includes a list of all inherited methods, with links. -* An 'Examples' section that contains all class and method examples. - This section is run by `R CMD check`, so method examples must work - without errors. +- An 'Examples' section that contains all class and method examples. + This section is run by `R CMD check`, so method examples must work without errors. An example from the R6 tutorial: @@ -320,7 +302,8 @@ public = list( ### Datasets -Datasets are usually stored as `.rdata` files in `data/` and not as regular R objects in the package. This means you need to document them slightly differently: instead of documenting the data directly, you quote the dataset's name. +Datasets are usually stored as `.rdata` files in `data/` and not as regular R objects in the package. +This means you need to document them slightly differently: instead of documenting the data directly, you quote the dataset's name. ```{r} #' Prices of 50,000 round cut diamonds @@ -348,16 +331,17 @@ Datasets are usually stored as `.rdata` files in `data/` and not as regular R ob Note the use of two additional tags that are particularly useful for documenting data: -* `@format`, which gives an overview of the structure of the dataset. - This should include a __definition list__ that describes each variable. - There's currently no way to generate this with Markdown, so this is - one of the few places you'll need to Rd markup directly. +- `@format`, which gives an overview of the structure of the dataset. + This should include a **definition list** that describes each variable. + There's currently no way to generate this with Markdown, so this is one of the few places you'll need to Rd markup directly. -* `@source` where you got the data form, often a URL. +- `@source` where you got the data form, often a URL. ### Packages -As well as documenting every object inside the package, you can also document the package itself by documenting the special sentinel `"_PACKAGE"`. We recommend placing package documentation in `{pkgname}-package.R`, and have `@keywords internal`. Here's an example: +As well as documenting every object inside the package, you can also document the package itself by documenting the special sentinel `"_PACKAGE"`. +We recommend placing package documentation in `{pkgname}-package.R`, and have `@keywords internal`. +Here's an example: ```{r, eval = FALSE} #' @details @@ -371,38 +355,36 @@ Package documentation is a good place to put `@section Package options:` that do Some notes: -* Package documentation will automatically include information parsed from the - `DESCRIPTION`, including title, description, list of authors, and useful - URLs. +- Package documentation will automatically include information parsed from the `DESCRIPTION`, including title, description, list of authors, and useful URLs. -* By default, aliases will be added so that both `?pkgname` and - `package?pkgname` will find the package help. If there's an existing - function called `pkgname`, use `@aliases {pkgname}-package` to - override the default. +- By default, aliases will be added so that both `?pkgname` and `package?pkgname` will find the package help. + If there's an existing function called `pkgname`, use `@aliases {pkgname}-package` to override the default. -* `usethis::use_package_doc()` will generate a basic template to get - you started. +- `usethis::use_package_doc()` will generate a basic template to get you started. -* Use `@references` to point to published material about the package that - users might find helpful. +- Use `@references` to point to published material about the package that users might find helpful. ## Sections -You can add arbitrary sections with the `@section` tag. This is a useful way of breaking a long details section into multiple chunks with useful headings. Section titles should be in sentence case, must fit on one line, and must be followed by a colon. +You can add arbitrary sections with the `@section` tag. +This is a useful way of breaking a long details section into multiple chunks with useful headings. +Section titles should be in sentence case, must fit on one line, and must be followed by a colon. ```{r} #' @section Warning: #' Do not operate heavy machinery within 8 hours of using this function. ``` -You can also create sections using the Markdown syntax for headers. For example, the previously-defined section can be created with Markdown headers like this: +You can also create sections using the Markdown syntax for headers. +For example, the previously-defined section can be created with Markdown headers like this: ```{r} #' @details # Warning #' Do not operate heavy machinery within 8 hours of using this function. ``` -Note that '`#`' may only appear after the `@description` and `@details` tags. Since `@details` can appear multiple times in a block, you can always precede a '`#`' section with `@details`. +Note that '`#`' may only appear after the `@description` and `@details` tags. +Since `@details` can appear multiple times in a block, you can always precede a '`#`' section with `@details`. To add a subsection, use level two or greater headings: @@ -418,24 +400,29 @@ If you find yourself adding a lot of sections, you might consider using a vignet ## Do repeat yourself -There is tension between the DRY (do not repeat yourself) principle of programming and the need for documentation to be self-contained. It's frustrating to have to navigate through multiple help files to pull together all the pieces you need. Roxygen2 provides several ways to avoid repeating yourself in code documentation, while assembling information from multiple places in one documentation file: +There is tension between the DRY (do not repeat yourself) principle of programming and the need for documentation to be self-contained. +It's frustrating to have to navigate through multiple help files to pull together all the pieces you need. +Roxygen2 provides several ways to avoid repeating yourself in code documentation, while assembling information from multiple places in one documentation file: -* Cross-link documentation files with `@seealso` and `@family`. +- Cross-link documentation files with `@seealso` and `@family`. -* Inherit documentation from another topic with - `@inherit`, `@inheritParams`, and `@inheritSection`. +- Inherit documentation from another topic with\ + `@inherit`, `@inheritParams`, and `@inheritSection`. -* Document multiple functions in the same topic with `@describeIn` or `@rdname`. +- Document multiple functions in the same topic with `@describeIn` or `@rdname`. -* Run arbitrary R code with the Markdown markup for inline code, see section 'Dynamic R code' in `vignette("rd-formatting")`. +- Run arbitrary R code with the Markdown markup for inline code, see section 'Dynamic R code' in `vignette("rd-formatting")`. -* Run arbitrary R code with `@eval`. +- Run arbitrary R code with `@eval`. -* Create reusable templates with `@template` and `@templateVar`. +- Create reusable templates with `@template` and `@templateVar`. ### Cross-references -There are two tags that make it easier for people to navigate your documentation: `@seealso` and `@family`. `@seealso` allows you to point to other useful resources, either on the web `` or to other documentation with `[function_name()]`. If you have a family of related functions, you can use `@family {family}` to cross-reference each function to every other function within the family. A function can be a member of multiple families. +There are two tags that make it easier for people to navigate your documentation: `@seealso` and `@family`. +`@seealso` allows you to point to other useful resources, either on the web `` or to other documentation with `[function_name()]`. +If you have a family of related functions, you can use `@family {family}` to cross-reference each function to every other function within the family. +A function can be a member of multiple families. For `sum()`, this might look like: @@ -445,7 +432,8 @@ For `sum()`, this might look like: #' [colSums()]/[rowSums()] marginal sums over high-dimensional arrays. ``` -By default `@family {family}`, will generate the see also text "Other {family}:", so the `@family` name should be plural (i.e., "model building helpers" not "model building helper"). You can override the default title by providing a `rd_family_title` list in `man/roxygen/meta.R`: +By default `@family {family}`, will generate the see also text "Other {family}:", so the `@family` name should be plural (i.e., "model building helpers" not "model building helper"). +You can override the default title by providing a `rd_family_title` list in `man/roxygen/meta.R`: ```{r, eval = FALSE} list( @@ -457,35 +445,35 @@ list( You can inherit documentation from other functions in a few ways: -* `@inherit source_function` will inherit parameters, return, references, - description, details, sections, and seealso from `source_function()`. - -* `@inherit source_function return details` will inherit selected components - from `source_function()` +- `@inherit source_function` will inherit parameters, return, references, description, details, sections, and seealso from `source_function()`. + +- `@inherit source_function return details` will inherit selected components from `source_function()` -* `@inheritParams source_function` inherits just the parameter documentation - from `source_function()`. +- `@inheritParams source_function` inherits just the parameter documentation from `source_function()`. + +- `@inheritSection source_function Section title` will inherit the single `@section` called "Section title" from `source_function()`. -* `@inheritSection source_function Section title` will inherit the single - `@section` called "Section title" from `source_function()`. - All of these work recursively so you can inherit documentation from a function that has inherited it from elsewhere. You can also inherit documentation from functions provided by another package by using `pkg::source_function`. ### Documenting multiple functions in the same file -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. +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. #### `@describeIn` `@describeIn` is designed for the most common cases: -* documenting methods in a generic -* documenting methods in a class -* documenting functions with the same (or similar arguments) +- documenting methods in a generic +- documenting methods in a class +- documenting functions with the same (or similar arguments) -It generates a new section, named either "Methods (by class)", "Methods (by generic)" or "Functions". The section contains a bulleted list describing each function, labelled so that you know what function or method it's talking about. Here's an example documenting an imaginary new generic: +It generates a new section, named either "Methods (by class)", "Methods (by generic)" or "Functions". +The section contains a bulleted list describing each function, labelled so that you know what function or method it's talking about. +Here's an example documenting an imaginary new generic: ```{r} #' Foo bar generic @@ -502,7 +490,11 @@ foobar.character <- function(x) paste0(x[1], "-", x[length(x)]) #### `@rdname` -`@rdname` is a more general-purpose tool. It overrides the default file name generated by roxygen and merges documentation for multiple objects into one file. This gives you complete freedom to combine documentation however you see fit. There are two ways to use `@rdname`. You can add documentation to an existing function: +`@rdname` is a more general-purpose tool. +It overrides the default file name generated by roxygen and merges documentation for multiple objects into one file. +This gives you complete freedom to combine documentation however you see fit. +There are two ways to use `@rdname`. +You can add documentation to an existing function: ```{r} #' Basic arithmetic @@ -532,7 +524,10 @@ times <- function(x, y) x * y ### Order of includes -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. +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. ```{r} #' @rdname arith @@ -546,7 +541,10 @@ times <- function(x, y) x * y ### Evaluating arbitrary code -Another technique is the `@eval` tag. It evaluates code and treatments the result as if it was a literal roxygen tags. This makes it possible to eliminate duplication by writing functions. The code will be evaluated in the package environment and should yield a character vector of roxygen comments (but without the leading `#'`). +Another technique is the `@eval` tag. +It evaluates code and treatments the result as if it was a literal roxygen tags. +This makes it possible to eliminate duplication by writing functions. +The code will be evaluated in the package environment and should yield a character vector of roxygen comments (but without the leading `#'`). For example, this code + roxygen block: @@ -578,9 +576,12 @@ foo <- function(x, y) { } ``` -Note that `@eval` cannot be embedded into another roxygen tag. If you want to dynamically generate part of a roxygen tag, see section 'Dynamic R code' in `vignette("rd-formatting")`. +Note that `@eval` cannot be embedded into another roxygen tag. +If you want to dynamically generate part of a roxygen tag, see section 'Dynamic R code' in `vignette("rd-formatting")`. -A related function is `@evalRd`. It works in the same way as `@eval` (i.e. it's evaluated in the package environment) but rather than yielding roxygen comments that are processed as if they had been typed directly, it yields top-level Rd code that is inserted directly into the generated `.Rd` file. It is primarily useful if you want to generate Rd structure that is not currently supported by roxygen2. +A related function is `@evalRd`. +It works in the same way as `@eval` (i.e. it's evaluated in the package environment) but rather than yielding roxygen comments that are processed as if they had been typed directly, it yields top-level Rd code that is inserted directly into the generated `.Rd` file. +It is primarily useful if you want to generate Rd structure that is not currently supported by roxygen2. For example, this block: @@ -598,7 +599,7 @@ NULL Would generate this Rd: -```latex +``` latex \note{ This is the first line This is the second line @@ -607,63 +608,72 @@ This is the second line ### Roxygen templates -Roxygen templates are R files that contain only roxygen comments and that live in the `man-roxygen` directory. Use `@template file-name` (without extension) to insert the contents of a template into the current documentation. +Roxygen templates are R files that contain only roxygen comments and that live in the `man-roxygen` directory. +Use `@template file-name` (without extension) to insert the contents of a template into the current documentation. -You can make templates more flexible by using template variables defined with `@templateVar name value`. Template files are run with brew, so you can retrieve values (or execute any other arbitrary R code) with `<%= name %>`. +You can make templates more flexible by using template variables defined with `@templateVar name value`. +Template files are run with brew, so you can retrieve values (or execute any other arbitrary R code) with `<%= name %>`. Note that templates are parsed a little differently to regular blocks, so you'll need to explicitly set the title, description and details with `@title`, `@description` and `@details`. ## Including external `.Rmd`/`.md` files -Starting from roxygen2 7.0.0, you can use `@includeRmd path/to/file.Rmd` to include an external `.Rmd` or `.md` document into a manual page (the path is relative from the source package root directory). You can include the same file in multiple documentation files, and for the first time, share content across documentation and vignettes. +Starting from roxygen2 7.0.0, you can use `@includeRmd path/to/file.Rmd` to include an external `.Rmd` or `.md` document into a manual page (the path is relative from the source package root directory). +You can include the same file in multiple documentation files, and for the first time, share content across documentation and vignettes. ### Sections -`@includeRmd` supports headings in the external Rmd. The rules are as follows: +`@includeRmd` supports headings in the external Rmd. +The rules are as follows: -* All text before the first level 1 heading (i.e. `#`), is added to the - details section. If you prefer a different section, then write the - name of the section after the path in the `@includeRmd` tag, in all - lowercase. Example: `@includeRmd path description`. This currently does - not work with user defined sections (created with `@section`). +- All text before the first level 1 heading (i.e. `#`), is added to the details section. + If you prefer a different section, then write the name of the section after the path in the `@includeRmd` tag, in all lowercase. + Example: `@includeRmd path description`. + This currently does not work with user defined sections (created with `@section`). -* Level 1 headings generate their own section (`\section{}`). +- Level 1 headings generate their own section (`\section{}`). -* Other headings (level 2 and so on) create subsections (`\subsection{}`) - within the section they appear in. +- Other headings (level 2 and so on) create subsections (`\subsection{}`) within the section they appear in. -All content in the Rmd file will go either in the details or in new top level sections. It is currently not possible to document function arguments, return values, etc. in external Rmd documents. +All content in the Rmd file will go either in the details or in new top level sections. +It is currently not possible to document function arguments, return values, etc. in external Rmd documents. ### Links -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. +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. ### Caching and figures -`@includeRmd` tries to set up knitr to support caching in the Rmd file. It sets the cache path to the default knitr cache path of the included Rmd file (i.e. `foo/bar/file_cache/` for `foo/bar/file.Rmd`), so if you do not change the cache path within the Rmd itself, then everything should work out of the box. You should add these cache paths to `.gitignore` and `.Rbuildignore`. +`@includeRmd` tries to set up knitr to support caching in the Rmd file. +It sets the cache path to the default knitr cache path of the included Rmd file (i.e. `foo/bar/file_cache/` for `foo/bar/file.Rmd`), so if you do not change the cache path within the Rmd itself, then everything should work out of the box. +You should add these cache paths to `.gitignore` and `.Rbuildignore`. -`@includeRmd` also sets the knitr figure path of the (`fig.path`) to the default figure path of the included Rmd. Overriding this default is unlikely to work. +`@includeRmd` also sets the knitr figure path of the (`fig.path`) to the default figure path of the included Rmd. +Overriding this default is unlikely to work. ### Sharing text between vignettes and the manual -`@includeRmd` helps avoiding repetition, as you can use the same `.Rmd` or `.md` document in the manual and also in the `README.md` file or in vignettes. One way to include an Rmd file in another one is to use child documents: +`@includeRmd` helps avoiding repetition, as you can use the same `.Rmd` or `.md` document in the manual and also in the `README.md` file or in vignettes. +One way to include an Rmd file in another one is to use child documents: -```` -```{r child = "common.Rmd"}`r ''` -``` -```` + ```{r child = "common.Rmd"}`r ''` + ``` -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 `@includeRmd`, which instead always links to the help topics in the manual. Example: +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 `@includeRmd`, which instead always links to the help topics in the manual. +Example: -``` -See also the [roxygen2::roxygenize()] function. + See also the [roxygen2::roxygenize()] function. -[roxygen2::roxygenize()]: https://roxygen2.r-lib.org/reference/roxygenize.html -``` + [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. +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. ## Other tags @@ -671,29 +681,27 @@ Note that if you add external link targets like these, then roxygen will emit a Three other tags make it easier for the user to find documentation within R's help system: -* Aliases form the index that `?` searches. Use - `@aliases space separated aliases` to add additional aliases. - -* `@concept` add extra keywords that will be found with `help.search()` +- Aliases form the index that `?` searches. + Use `@aliases space separated aliases` to add additional aliases. + +- `@concept` add extra keywords that will be found with `help.search()` -* Use `@keywords keyword1 keyword2 ...` to add standardised keywords. - Keywords are optional, but if present, must be taken from the - predefined list found `file.path(R.home("doc"), "KEYWORDS")`. +- Use `@keywords keyword1 keyword2 ...` to add standardised keywords. + Keywords are optional, but if present, must be taken from the predefined list found `file.path(R.home("doc"), "KEYWORDS")`. -Apart from `@keywords internal`, these tags are not very useful because most people find documentation using Google. `@keywords internal` is useful because it removes the function from the documentation index; it's useful for functions aimed primarily at other developers, not typical users of the package. +Apart from `@keywords internal`, these tags are not very useful because most people find documentation using Google. +`@keywords internal` is useful because it removes the function from the documentation index; it's useful for functions aimed primarily at other developers, not typical users of the package. ### Back references -The original source location is added as a comment -to the second line of each generated `.Rd` file in the following form: +The original source location is added as a comment to the second line of each generated `.Rd` file in the following form: -``` -% Please edit documentation in ... -``` + % Please edit documentation in ... `roxygen2` tries to capture all locations from which the documentation is assembled. -For code that *generates* R code with Roxygen comments (e.g., the Rcpp package), -the `@backref` tag is provided. This allows specifying the "true" source of the documentation, and will substitute the default list of source files. Use one tag per source file: +For code that *generates* R code with Roxygen comments (e.g., the Rcpp package), the `@backref` tag is provided. +This allows specifying the "true" source of the documentation, and will substitute the default list of source files. +Use one tag per source file: ```{r} #' @backref src/file.cpp