|
| 1 | +--- |
| 2 | +name: document |
| 3 | +trigger: document functions |
| 4 | +description: Document package functions. Use when asked to document functions. |
| 5 | +--- |
| 6 | + |
| 7 | +# Document functions |
| 8 | + |
| 9 | +*All* R functions in `R/` should be documented in roxygen2 `#'` style, including internal/unexported functions. |
| 10 | + |
| 11 | +- Run `air format .` then `devtools::document()` after changing any roxygen2 docs. |
| 12 | +- Use sentence case for all headings. |
| 13 | +- Wrap roxygen comments at 80 characters. |
| 14 | +- Files matching `R/import-standalone-*.R` are imported from other packages and have their own conventions. Do not modify their documentation. |
| 15 | +- After documenting functions, run `devtools::document(roclets = c('rd', 'collate', 'namespace'))`. |
| 16 | +- If `_pkgdown.yml` exists and contains a `reference` section: |
| 17 | + - Whenever you add a new (non-internal) documentation topic, also add the topic to `_pkgdown.yml`. |
| 18 | + - Use `pkgdown::check_pkgdown()` to check that all topics are included in the reference index. |
| 19 | + |
| 20 | +## Shared parameters |
| 21 | + |
| 22 | +**Parameters used in more than one function** go in `R/aaa-shared_params.R` under `@name .shared-params`. Functions inherit them with `@inheritParams .shared-params`. See `R/aaa-shared_params.R` for current definitions (if it exists). |
| 23 | + |
| 24 | +Shared params blocks: alphabetize parameters, use `@name .shared-params` (with leading dot), include `@keywords internal`, end with `NULL`. |
| 25 | + |
| 26 | +Multiple shared-params groups (e.g. `.shared-params-io`, `.shared-params-parsing`) are appropriate when parameters are only shared within a file and closely related files. |
| 27 | + |
| 28 | +## Parameter documentation format |
| 29 | + |
| 30 | +```r |
| 31 | +#' @param param_name (`TYPE`) Brief description (usually 1-3 sentences. Can |
| 32 | +#' include [cross_references()]. Additional details on continuation lines if |
| 33 | +#' needed. |
| 34 | +``` |
| 35 | + |
| 36 | +Function-specific `@param` definitions always appear *before* any `@inheritParams` lines. If all parameters are defined locally, omit `@inheritParams` entirely. |
| 37 | + |
| 38 | +### Type notation |
| 39 | + |
| 40 | +| Notation | Meaning | |
| 41 | +|----------|---------| |
| 42 | +| ``(`character`)`` | Character vector | |
| 43 | +| ``(`character(1)`)`` | Single string | |
| 44 | +| ``(`logical(1)`)`` | Single logical | |
| 45 | +| ``(`integer`)`` | Integer vector | |
| 46 | +| ``(`integer(1)`)`` | Single integer | |
| 47 | +| ``(`double`)`` | Double vector | |
| 48 | +| ``(`vector(0)`)`` | A prototype (zero-length vector) | |
| 49 | +| ``(`vector`)`` | A vector of unspecified type | |
| 50 | +| ``(`list`)`` | List | |
| 51 | +| ``(`data.frame`)`` | Data frame or tibble | |
| 52 | +| ``(`function` or `NULL`)`` | A function or NULL | |
| 53 | +| ``(`my_class`)`` | A class-specific type (use the actual class name) | |
| 54 | +| ``(`any`)`` | Any type | |
| 55 | + |
| 56 | +### Enumerated values |
| 57 | + |
| 58 | +When a parameter takes one of a fixed set of values, document them with a bullet list: |
| 59 | + |
| 60 | +```r |
| 61 | +#' @param method (`character(1)`) The aggregation method. Can be one of: |
| 62 | +#' * `"mean"`: Arithmetic mean. |
| 63 | +#' * `"median"`: Median value. |
| 64 | +#' * `"sum"`: Total sum. |
| 65 | +``` |
| 66 | + |
| 67 | +## Returns |
| 68 | + |
| 69 | +Use `@returns` (not `@return`). Include a type when it's informative: |
| 70 | + |
| 71 | +```r |
| 72 | +#' @returns A summary tibble. |
| 73 | +#' @returns (`logical(1)`) `TRUE` if `x` is a valid record. |
| 74 | +#' @returns Either a tibble or a list, depending on the input. |
| 75 | +#' @returns `NULL` (invisibly). |
| 76 | +``` |
| 77 | + |
| 78 | +**Structured returns with columns:** |
| 79 | + |
| 80 | +```r |
| 81 | +#' @returns A [tibble::tibble()] with columns: |
| 82 | +#' - `name`: Record name. |
| 83 | +#' - `value`: Numeric value. |
| 84 | +#' - `status`: Status (`"active"` or `"inactive"`). |
| 85 | +``` |
| 86 | + |
| 87 | +## Exported functions |
| 88 | + |
| 89 | +```r |
| 90 | +#' Title in sentence case |
| 91 | +#' |
| 92 | +#' Description paragraph providing context and details. |
| 93 | +#' |
| 94 | +#' @param param_name (`TYPE`) Description. |
| 95 | +#' @inheritParams .shared-params |
| 96 | +#' |
| 97 | +#' @returns Description of return value. |
| 98 | +#' @seealso [related_function()] |
| 99 | +#' @export |
| 100 | +#' |
| 101 | +#' @examples |
| 102 | +#' example_code() |
| 103 | +``` |
| 104 | + |
| 105 | +- Blank `#'` lines separate: title/description, description/params, and `@export`/`@examples`. |
| 106 | +- `@seealso` (optional) goes between `@returns` and `@export`. |
| 107 | +- `@details` can supplement the description when needed. |
| 108 | + |
| 109 | +## Internal (unexported) functions |
| 110 | + |
| 111 | +Internal helpers (identified by a dot prefix, e.g. `.parse_response()`) use abbreviated documentation. Mark them with `@keywords internal` and omit `@export`: |
| 112 | + |
| 113 | +```r |
| 114 | +#' Title in sentence case |
| 115 | +#' |
| 116 | +#' @param one_off_param (`TYPE`) Description. |
| 117 | +#' @inheritParams .shared-params |
| 118 | +#' @returns (`TYPE`) What it returns. |
| 119 | +#' @keywords internal |
| 120 | +``` |
| 121 | + |
| 122 | +Description paragraph is optional (only include when usage isn't obvious), fewer blank `#'` lines, and no `@examples`. |
| 123 | + |
| 124 | +## S3 methods and `@rdname` grouping |
| 125 | + |
| 126 | +Use `@rdname` to group related functions under one help page. This applies to: |
| 127 | +- **S3 methods we own** (generic defined in this package): generic gets full docs, methods get `@rdname` + `@export`. |
| 128 | +- **Related exported functions** (e.g. multiple variants of the same operation): primary function gets full docs, variants get `@rdname` + `@export`. |
| 129 | + |
| 130 | +```r |
| 131 | +#' Format a summary object |
| 132 | +#' |
| 133 | +#' @param x (`any`) The object to format. |
| 134 | +#' @param ... Additional arguments passed to methods. |
| 135 | +#' @returns A formatted character string. |
| 136 | +#' @keywords internal |
| 137 | +.format_summary <- function(x, ...) { |
| 138 | + UseMethod(".format_summary") |
| 139 | +} |
| 140 | + |
| 141 | +#' @rdname .format_summary |
| 142 | +#' @export |
| 143 | +.format_summary.data_summary <- function(x, ...) { |
| 144 | + # method implementation |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +**S3 methods we don't own** (generic from another package) need standalone documentation: |
| 149 | + |
| 150 | +```r |
| 151 | +#' Title describing the method |
| 152 | +#' |
| 153 | +#' @param x (`TYPE`) Description. |
| 154 | +#' @param ... Additional arguments (ignored). |
| 155 | +#' @returns Description. |
| 156 | +#' @exportS3Method pkg::generic |
| 157 | +method.class <- function(x, ...) { ... } |
| 158 | +``` |
| 159 | + |
| 160 | +## Style notes |
| 161 | + |
| 162 | +**Cross-references:** Use square brackets — `[fetch_records()]` (internal), `[tibble::tibble()]` (external), `[topic_name]` (topics). |
| 163 | + |
| 164 | +**Section comment headers** optionally organize code within a file, lowercase with dashes to column 80: |
| 165 | + |
| 166 | +```r |
| 167 | +# helpers ---------------------------------------------------------------------- |
| 168 | +``` |
| 169 | + |
| 170 | +Only use such headers in complex files. The need for section comment headers might indicate that the file should be split into multiple files. |
| 171 | + |
| 172 | +**Examples:** Exported functions include `@examples`. Use `@examplesIf interactive()` for network-dependent or slow functions. Use section-style comments (`# Section ---`) to organize longer example blocks. Internal functions do not get examples. |
0 commit comments