| Title: | Typed Parameter Tags for Integration with 'roxygen2' |
|---|---|
| Description: | Provides typed parameter documentation tags for integration with 'roxygen2'. Typed parameter tags provide a consistent interface for annotating expected types for parameters and returned values. Tools for converting from existing styles are also provided to easily adapt projects which implement typed documentation by convention rather than tag. Use the default format or provide your own. |
| Authors: | Doug Kelkhoff [aut, cre] |
| Maintainer: | Doug Kelkhoff <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.2 |
| Built: | 2026-05-21 10:20:26 UTC |
| Source: | https://github.com/openpharma/roxytypes |
Allow glue-style formatting using keyworded regular expressions. The
original glue string (anything that isn't expanded by glue) is treated as
a string literal, whereas the contents of populated values can be regular
expressions, allowing for a more user-friendly way to construct complicated
regular expressions.
build_format_regex( format, format_re, ..., type = re_backticked(), description = re_any() ) re_backticked() re_any() escape_non_glue_re(x)build_format_regex( format, format_re, ..., type = re_backticked(), description = re_any() ) re_backticked() re_any() escape_non_glue_re(x)
format |
( |
format_re |
( |
... |
Additional arguments provide keyworded capture groups for |
type |
( |
description |
( |
x |
( |
To bypass glue entirely and use a standard regular expression, use
format_re.
The provided regular expression must match all characters from the start of a
string to the end. The string also matches using "dot all" syntax, meaning
that the . expression will also match newline characters.
(character[1]:) A regular expression string, built from component sub-expressions.
re_backticked(): Match within backticks
re_any(): Match any
escape_non_glue_re(): Escape all regular expression special characters
In addition, avoid escaping {}'s that appear to be used as glue keywords.
Handles only simple cases, and does not handle recusive curly nesting.
re <- roxytypes:::build_format_regex( "{as}{any}{bs}", as = "a+", bs = "b+", any = ".*?" ) roxytypes:::regex_capture(re, "aaaa\n\nbb", perl = TRUE) text <- "@param (`test(\")\")`)" pattern <- sprintf("`%s`", re_backticked()) m <- regexec(pattern, text, perl = TRUE) regmatches(text, m)[[1]] # [1] "`test(\")\")`" # curlies escaped, as this does not appear to be a glue-style usage roxytypes:::escape_non_glue_re(".{1,3}") # curlies not escaped, as this is a glue-style usage roxytypes:::escape_non_glue_re("this is a {test}")re <- roxytypes:::build_format_regex( "{as}{any}{bs}", as = "a+", bs = "b+", any = ".*?" ) roxytypes:::regex_capture(re, "aaaa\n\nbb", perl = TRUE) text <- "@param (`test(\")\")`)" pattern <- sprintf("`%s`", re_backticked()) m <- regexec(pattern, text, perl = TRUE) regmatches(text, m)[[1]] # [1] "`test(\")\")`" # curlies escaped, as this does not appear to be a glue-style usage roxytypes:::escape_non_glue_re(".{1,3}") # curlies not escaped, as this is a glue-style usage roxytypes:::escape_non_glue_re("this is a {test}")
roxygen2 tags to roxytypes tagsConvert a package codebase into applicable roxytypes tags. For roxygen2
tags with drop-in replacements (namely @param and @return tags), process
descriptions and replace tags with roxytypes equivalents.
convert( path = ".", format = config(path, refresh = TRUE, cache = FALSE)$format, ..., unmatched = FALSE, verbose = interactive() )convert( path = ".", format = config(path, refresh = TRUE, cache = FALSE)$format, ..., unmatched = FALSE, verbose = interactive() )
path |
( |
format |
( |
... |
Additional arguments passed to |
unmatched |
( |
verbose |
( |
A format string is built using build_format_regex(), which accepts
parameters type and description, which describe how to match these
components of a parameter definition. They are combined with the literal
content of format to produce a regular expression to split existing
definitions.
For comprehensive control, pass format_re directly, bypassing expression
construction altogether.
(logical[1]) TRUE if successfully completes, FALSE if aborted. Always returns
invisibly.
## Not run: convert("(`{type}`) {description}") ## End(Not run)## Not run: convert("(`{type}`) {description}") ## End(Not run)
@typed
Adds special cases for when the type uses other roxygen2 syntax
default_format(x, name, type, description, ...)default_format(x, name, type, description, ...)
x |
( |
name, type, description
|
( |
... |
Additional arguments unused. |
A formatted character value.
A helper to reliably read DCF files
read_dcf_asis(path)read_dcf_asis(path)
path |
( |
(data.frame) The result of read.dcf().
roxygen2 @typed tag parsingParse a @typed tag and return parsed components as value
## S3 method for class 'roxy_tag_typed' roxy_tag_parse(x)## S3 method for class 'roxy_tag_typed' roxy_tag_parse(x)
x |
A tag |
(roxygen2 tag) A parsed roxygen2 @typed rd_tag.
roxygen2 @typedreturn tag parsingParse a @typedreturn tag and return parsed components as value
## S3 method for class 'roxy_tag_typedreturn' roxy_tag_parse(x)## S3 method for class 'roxy_tag_typedreturn' roxy_tag_parse(x)
x |
A tag |
(roxygen2 tag) A parsed roxygen2 @typedreturn rd_tag.
roxygen2 @typed tag rd section populationPush typed fields into @param section
## S3 method for class 'roxy_tag_typed' roxy_tag_rd(x, base_path, env)## S3 method for class 'roxy_tag_typed' roxy_tag_rd(x, base_path, env)
x |
The tag |
base_path |
Path to package root directory. |
env |
Environment in which to evaluate code (if needed) |
(roxygen2::rd_section) A roxygen2 "param" rd_section with formatted type information.
roxygen2 @typedreturn tag rd section populationPush typed fields into @param section
## S3 method for class 'roxy_tag_typedreturn' roxy_tag_rd(x, base_path, env)## S3 method for class 'roxy_tag_typedreturn' roxy_tag_rd(x, base_path, env)
x |
The tag |
base_path |
Path to package root directory. |
env |
Environment in which to evaluate code (if needed) |
(roxygen2::rd_section) A roxygen2 @value rd_tag with formatted type information.
roxytypes tagsThe @typed tag introduces a syntax for defining parameter types as a
roxygen2 tag.
#' @typed <var>: <type> #' <description>#' @typed <var>: <type> #' <description>
Be aware that there are a few syntactic requirements:
: delimiter between the variable name and type.
\n after the type to separate it from the description.
type Parsing SyntaxThe type portion of the @typed tag syntax will handle a bit of syntax as
special cases.
[type]: Types wrapped in brackets, for example
[roxygen2::roxy_tags()] will be left as-is, without wrapping the string
in backticks to display as inline code and preserve the native roxygen2
reference link.
#' @typed arg: [package::function()] #' long form description.
`type`: Types wrapped in backticks will be kept as-is. Additional
backticks will not be inserted.
#' @typed arg: `class` #' long form description.
"type" or 'type': Types wrapped in quotes (either single or double),
will be provided as literal values, removing the surrounding quotation
marks.
#' @typed arg: "`class_a` or `class_b`" #' depending on the class of the object provided, either an `"A"` #' or a `"B"`.
type Parsing FunctionThe above defaults are meant to cover most use cases and should be sufficient for all but the most elaborate development practices. If you need to go beyond these default behaviors, you can also provide a parsing function, accepting the parsed roxygen tag as well as the raw contents.
The function accepts the roxygen2::roxy_tag() produced when parsing the
tag, whose $val contains fields name, type and description. For
convenience, the $val contents is unpacked as arguments, though the
structure of this tag is liable to change.
To implement a typescript-style class union syntax,
#' @typed arg: class_a | class_b | class_c #' depending on the class of the object provided, either an `"A"` #' or a `"B"`.
to produce the parameter definition
(`class_a`, `class_c` or `class_b`) depending on the class of the object provided, either an `"A"`, `"B"` or a `"C"`.
we might define the following in DESCRIPTION (or in
man/roxytypes/meta.R).
Config/roxytypes: list(
format = function(tag, ..., name, type, description) {
types <- paste0("`", trimws(strsplit(type, "|", fixed = TRUE)[[1]]), "`")
types <- glue::glue_collapse(types, sep = ", ", last = " or ")
paste0("(", types, ") ", description)
}
)
roxygen2 @typedreturn tagThe @typedreturn tag introduces a syntax for defining return types as a
roxygen2 tag.
#' @typedreturn <type> #' <description>#' @typedreturn <type> #' <description>
There is one important syntactic features:
\n after the type to separate it from the description.
A helper to apply field names to all roxy_tag val fields
with_roxy_field_subclass(x)with_roxy_field_subclass(x)
x |
( |
(: list) A nearly identical list, where elements have additional subclasses based
on their field names.