Skip to content

Commit ad628d3

Browse files
authored
Reformat vignettes with panache (#1854)
Keeping it manual for now
1 parent 3aae6ef commit ad628d3

10 files changed

Lines changed: 411 additions & 372 deletions

File tree

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
^\.claude$
2626
^[.]?air[.]toml$
2727
^\.vscode$
28+
^panache\.toml$

panache.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[format]
2+
wrap = "sentence"
3+
4+
[formatters]
5+
r = "air"

vignettes/extending.Rmd

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ vignette: >
77
%\VignetteEncoding{UTF-8}
88
---
99

10-
```{r, include = FALSE}
10+
```{r}
11+
#| include: false
1112
knitr::opts_chunk$set(comment = "#>", collapse = TRUE)
1213
```
1314

1415
roxygen2 is extensible, and this vignette will show you how.
15-
It starts with an introduction to the basic workflow of `roxygenize()` and the key data structures that power it. Then we'll show you how you can use its two extension points:
16+
It starts with an introduction to the basic workflow of `roxygenize()` and the key data structures that power it.
17+
Then we'll show you how you can use its two extension points:
1618

17-
- Add a new tag to generate a new top-level section in an `.Rd` file. This allows you to repeat yourself less when documenting your package. (See `vignette("reuse")` for other techniques.)
19+
- Add a new tag to generate a new top-level section in an `.Rd` file.
20+
This allows you to repeat yourself less when documenting your package.
21+
(See `vignette("reuse")` for other techniques.)
1822

19-
- Add a new roclet. This lets you take full advantage of the computational machinery behind `roxygenize()` to compute anything you want or produce any artefact you can imagine.
23+
- Add a new roclet.
24+
This lets you take full advantage of the computational machinery behind `roxygenize()` to compute anything you want or produce any artefact you can imagine.
2025

21-
```{r setup}
26+
```{r}
27+
#| label: setup
2228
library(roxygen2)
2329
```
2430

@@ -29,11 +35,15 @@ But if you're going to extend roxygen2, you'll need to know exactly what's happe
2935

3036
- Loads the package under roxygenizing, as well as any further packages ("packages" in `load_options()`).
3137

32-
- Parses all R files in the package, using available tags.
38+
- Parses all R files in the package, using available tags.
3339

34-
- Finds the roclets (see `roclet()`) from its `roclets` argument or the "roclets" option (`load_options()`). It defaults to using the Collate "roclet", the Rd roclet, and NAMESPACE roclet, but you can also add your own.
40+
- Finds the roclets (see `roclet()`) from its `roclets` argument or the "roclets" option (`load_options()`).
41+
It defaults to using the Collate "roclet", the Rd roclet, and NAMESPACE roclet, but you can also add your own.
3542

36-
- Runs the different methods of all those roclets, in order and independently: clean (`roclet_clean`), preprocess (`roclet_preprocess()`), process (`roclet_process()`), and output (`roclet_output()`). Only process and output are routinely used. For example, if you think of the Rd roclet, its process method digests information from the tag parsing, combines inherits, etc. to create the content of each documentation topic, and its output method writes those topics to disk.
43+
- Runs the different methods of all those roclets, in order and independently: clean (`roclet_clean`), preprocess (`roclet_preprocess()`), process (`roclet_process()`), and output (`roclet_output()`).
44+
Only process and output are routinely used.
45+
For example, if you think of the Rd roclet, its process method digests information from the tag parsing, combines inherits, etc.
46+
to create the content of each documentation topic, and its output method writes those topics to disk.
3747

3848
## Key data structures
3949

@@ -44,14 +54,15 @@ Before we dive into extending roxygen2, we need to first discuss two important d
4454
A tag (a list with S3 class `roxy_tag`) represents a single tag.
4555
It has the following fields:
4656

47-
- `tag`: the name of the tag.
57+
- `tag`: the name of the tag.
4858

49-
- `raw`: the raw contents of the tag (i.e. everything from the end of this tag to the beginning of the next).
59+
- `raw`: the raw contents of the tag (i.e.
60+
everything from the end of this tag to the beginning of the next).
5061

51-
- `val`: the parsed value, which we'll come back to shortly.
62+
- `val`: the parsed value, which we'll come back to shortly.
5263

53-
- `file` and `line`: the location of the tag in the package.
54-
Used with `roxy_tag_warning()` to produce informative error messages.
64+
- `file` and `line`: the location of the tag in the package.
65+
Used with `roxy_tag_warning()` to produce informative error messages.
5566

5667
You *can* construct tag objects by hand with `roxy_tag()`:
5768

@@ -67,10 +78,10 @@ However, you should rarely need to do so (except in tests), because you'll typic
6778
A block (a list with S3 class `roxy_block`) represents a single roxygen block.
6879
It has the following fields:
6980

70-
- `tags`: a list of `roxy_tags`.
71-
- `call`: the R code associated with the block (usually a function call).
72-
- `file` and `line`: the location of the R code.
73-
- `object`: the evaluated R object associated with the code.
81+
- `tags`: a list of `roxy_tags`.
82+
- `call`: the R code associated with the block (usually a function call).
83+
- `file` and `line`: the location of the R code.
84+
- `object`: the evaluated R object associated with the code.
7485

7586
The easiest way to see the basic structure of a `roxy_block()` is to generate one by parsing a roxygen block with `parse_text()`:
7687

@@ -92,21 +103,21 @@ block
92103

93104
You'll notice that some of the tags didn't exist in the original block:
94105

95-
* `@title` and `@description` are extracted from the text that appears before the first explicit tag.
96-
* `@usage` is generated automatically from the function formals.
97-
* `@.formals` is an "internal" tag that doesn't generate any output but is used to pass some important data around.
98-
* `@backref` stores the source location of the block so we can later record which `.R` files contributed to each `.Rd` file.
106+
- `@title` and `@description` are extracted from the text that appears before the first explicit tag.
107+
- `@usage` is generated automatically from the function formals.
108+
- `@.formals` is an "internal" tag that doesn't generate any output but is used to pass some important data around.
109+
- `@backref` stores the source location of the block so we can later record which `.R` files contributed to each `.Rd` file.
99110

100111
## Adding a new `.Rd` tag
101112

102113
The most common way to extend roxygen2 is to create a new tag that adds output to `.Rd` files.
103114
This requires defining a few methods:
104115

105-
1. Define a `roxy_tag_parse()` method that describes how to parse our new tag.
116+
1. Define a `roxy_tag_parse()` method that describes how to parse our new tag.
106117

107-
2. Define a `roxy_tag_rd()` method that describes how to convert the tag into `.Rd` commands.
118+
2. Define a `roxy_tag_rd()` method that describes how to convert the tag into `.Rd` commands.
108119

109-
3. If the tag's content is meant to appear in a custom section (as opposed to, say, the examples section), define a `format()` method that describes how to create the `.Rd` string.
120+
3. If the tag's content is meant to appear in a custom section (as opposed to, say, the examples section), define a `format()` method that describes how to create the `.Rd` string.
110121

111122
To illustrate the basic idea, we'll create a new `@tip` tag that will create a bulleted list of tips about how to use a function.
112123
The idea is to take something like this:
@@ -118,7 +129,7 @@ The idea is to take something like this:
118129

119130
That generates Rd like this:
120131

121-
``` latex
132+
```latex
122133
\section{Tips and tricks}{
123134
\itemize{
124135
\item The mean of a logical vector is the proportion of \code{TRUE} values.
@@ -140,7 +151,8 @@ roxy_tag_parse.roxy_tag_tip <- function(x) {
140151

141152
(There are lots of other built in options that you can read about in `?tag_markdown`.)
142153

143-
```{r, include = FALSE}
154+
```{r}
155+
#| include: false
144156
# Needed for vignette
145157
registerS3method("roxy_tag_parse", "roxy_tag_tip", roxy_tag_parse.roxy_tag_tip)
146158
```
@@ -176,7 +188,8 @@ roxy_tag_rd.roxy_tag_tip <- function(x, base_path, env) {
176188
}
177189
```
178190

179-
```{r, include = FALSE}
191+
```{r}
192+
#| include: false
180193
# Needed for vignette
181194
registerS3method("roxy_tag_rd", "roxy_tag_tip", roxy_tag_rd.roxy_tag_tip)
182195
```
@@ -202,7 +215,8 @@ format.rd_section_tip <- function(x, ...) {
202215
}
203216
```
204217

205-
```{r, include = FALSE}
218+
```{r}
219+
#| include: false
206220
# Needed for vignette
207221
registerS3method("format", "rd_section_tip", format.rd_section_tip)
208222
```
@@ -218,12 +232,14 @@ Note that there is no namespacing so if you're defining multiple new tags I reco
218232

219233
### Using your new tag
220234

221-
Now that the three methods are created, we still need to make them available to `roxygenize()`. First, you need to export the method:
235+
Now that the three methods are created, we still need to make them available to `roxygenize()`.
236+
First, you need to export the method:
222237

223-
* If the package defining the tag _imports_ roxygen2, use `@export`.
224-
* If the package defining the tag only _suggests_ roxygen2, use `@exportMethod`.
238+
- If the package defining the tag *imports* roxygen2, use `@export`.
239+
- If the package defining the tag only *suggests* roxygen2, use `@exportMethod`.
225240

226-
Next, you'll need to load the tag-defining package in the package where you want to use it. For example, if you created some new tags in {packageFoo}, and would like to use these tags in your documentation for {packageBar}, append this line to the `DESCRIPTION` of {packageBar}:
241+
Next, you'll need to load the tag-defining package in the package where you want to use it.
242+
For example, if you created some new tags in {packageFoo}, and would like to use these tags in your documentation for {packageBar}, append this line to the `DESCRIPTION` of {packageBar}:
227243

228244
```
229245
Config/roxygen2/packages: packageFoo
@@ -237,7 +253,8 @@ Creating a new roclet is usually a two part process.
237253
First, you define new tags that your roclet will work with, unless your roclet only needs information from existing tags, or only needs the path to the package source[^vignette].
238254
Second, you define a roclet that tells `roxygenize()` what to compute and produce based on this information.
239255

240-
[^vignette]: For example, the no-longer recommended `vignette_roclet()` only needs the path to the package source as input; it does not use information from the tag parsing step. Or the {roxylint} package only uses existing tags; its job is to warn you about suboptimal roxygen2 style.
256+
[^vignette]: For example, the no-longer recommended `vignette_roclet()` only needs the path to the package source as input; it does not use information from the tag parsing step.
257+
Or the {roxylint} package only uses existing tags; its job is to warn you about suboptimal roxygen2 style.
241258

242259
### Custom tags
243260

@@ -254,7 +271,8 @@ For example:
254271
#' @memo [EFFICIENCY] Currently brute-force; find better algorithm.
255272
```
256273

257-
As above, we first define a parse method. This time we use custom format based on a regular expression:
274+
As above, we first define a parse method.
275+
This time we use custom format based on a regular expression:
258276

259277
```{r}
260278
roxy_tag_parse.roxy_tag_memo <- function(x) {
@@ -273,7 +291,8 @@ roxy_tag_parse.roxy_tag_memo <- function(x) {
273291
}
274292
```
275293

276-
```{r, include = FALSE}
294+
```{r}
295+
#| include: false
277296
# Needed for vignette
278297
registerS3method(
279298
"roxy_tag_parse",
@@ -316,9 +335,9 @@ memo_roclet <- function() {
316335
To give the roclet behaviour, you need to define methods.
317336
There are two methods that almost every roclet will use:
318337

319-
- `roclet_process()` is called with a list of blocks, and returns an object of your choosing.
338+
- `roclet_process()` is called with a list of blocks, and returns an object of your choosing.
320339

321-
- `roclet_output()` produces side-effects (usually writing to disk) using the result from `roclet_process()`.
340+
- `roclet_output()` produces side-effects (usually writing to disk) using the result from `roclet_process()`.
322341

323342
For this roclet, we'll have `roclet_process()` collect all the memo tags into a named list:
324343

@@ -353,7 +372,8 @@ roclet_output.roclet_memo <- function(x, results, base_path, ...) {
353372
}
354373
```
355374

356-
```{r, include = FALSE}
375+
```{r}
376+
#| include: false
357377
# Needed for vignette
358378
registerS3method("roclet_process", "roclet_memo", roclet_process.roclet_memo)
359379
registerS3method("roclet_output", "roclet_memo", roclet_output.roclet_memo)
@@ -388,7 +408,8 @@ To use a roclet when developing a package, call
388408
roxygen2::roxygenize(roclets = "yourPackage::roclet")
389409
```
390410

391-
where `yourPackage::roclet` is the function which creates the roclet, e.g. `memo_roclet` above.
411+
where `yourPackage::roclet` is the function which creates the roclet, e.g.
412+
`memo_roclet` above.
392413

393414
You can also add the roclet to the target package's DESCRIPTION file, like this:
394415

@@ -398,7 +419,8 @@ Config/roxygen2/roclets: collate, rd, namespace, yourPackage::roclet
398419

399420
See `load_options()` for more details.
400421

401-
Your package only needs to be installed when the user documents the package, which doesn't correspond precisely to any field in the `DESCRIPTION`. However, you can think of it as a development dependency and hence put it in the `Suggests:` field:
422+
Your package only needs to be installed when the user documents the package, which doesn't correspond precisely to any field in the `DESCRIPTION`.
423+
However, you can think of it as a development dependency and hence put it in the `Suggests:` field:
402424

403425
```r
404426
usethis::use_package("yourPackage", type = "Suggests")
@@ -411,5 +433,5 @@ You don't have to do this, but it will help other developers working on the targ
411433
This vignette is quite rough, so you might want to also read some roxygen2 source code to understand all the extension points.
412434

413435
Do not hesitate to also look for examples of roxygen2 extensions.
414-
For instance, the [roxygenlabs](https://github.com/gaborcsardi/roxygenlabs) package (former incubator of roxygen2 features) used a third extension point: it _extended_ the Rd roclet with further methods, thus creating a supercharged Rd roclet rather than a brand-new roclet.
436+
For instance, the [roxygenlabs](https://github.com/gaborcsardi/roxygenlabs) package (former incubator of roxygen2 features) used a third extension point: it *extended* the Rd roclet with further methods, thus creating a supercharged Rd roclet rather than a brand-new roclet.
415437
Or, the [plumber2](https://github.com/posit-dev/plumber2) package only uses the parsing features from roxygen2, and does not use `roxygenize()` at all.

vignettes/index-crossref.Rmd

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ vignette: >
1111
%\VignetteEncoding{UTF-8}
1212
---
1313

14-
```{r, include = FALSE}
14+
```{r}
15+
#| include: false
1516
knitr::opts_chunk$set(
1617
collapse = TRUE,
1718
comment = "#>"
@@ -39,7 +40,8 @@ By default `@family {family}`, will generate the see also text "Other {family}:"
3940

4041
If you want to override the default title, you can provide an `rd_family_title` element in a list stored in `man/roxygen/meta.R`:
4142

42-
```{r, eval = FALSE}
43+
```{r}
44+
#| eval: false
4345
list(
4446
rd_family_title = list(aggregations = "Aggregation functions")
4547
)
@@ -60,10 +62,10 @@ You can add additional aliases with `@aliases alias1 alias2 alias3` or remove de
6062

6163
As well as looking in the aliases, `help.search()` and `???` also look in the `@title`, `@keywords`, and `@concept`s tags.
6264

63-
- `@keywords` adds standard keywords, which must be present in `file.path(R.home("doc"), "KEYWORDS")`.
65+
- `@keywords` adds standard keywords, which must be present in `file.path(R.home("doc"), "KEYWORDS")`.
6466

65-
- `@concept` adds arbitrary key words or phrases.
66-
Each `@concept` should contain a single word or phrase.
67+
- `@concept` adds arbitrary key words or phrases.
68+
Each `@concept` should contain a single word or phrase.
6769

6870
Generally speaking, `@keywords` and `@concepts` are not terribly useful because most people find documentation using Google, not R's built-in search.
6971

@@ -74,7 +76,9 @@ It's useful because it removes the function from the documentation index; it's u
7476

7577
The original source location is added as a comment to the second line of each generated `.Rd` file in the following form:
7678

77-
% Please edit documentation in ...
79+
```
80+
% Please edit documentation in ...
81+
```
7882

7983
`roxygen2` tries to capture all locations from which the documentation is assembled.
8084
For code that *generates* R code with Roxygen comments (e.g., the Rcpp package), the `@backref` tag is provided.

0 commit comments

Comments
 (0)