@@ -18,6 +18,7 @@ can use its two extension points:
1818 to compute anything you want or produce any artefact you can imagine.
1919
2020``` r
21+
2122library(roxygen2 )
2223```
2324
@@ -80,6 +81,7 @@ You *can* construct tag objects by hand with
8081[ ` roxy_tag() ` ] ( https://roxygen2.r-lib.org/dev/reference/roxy_tag.md ) :
8182
8283``` r
84+
8385roxy_tag(" name" , " Hadley" )
8486# > [????:???] @name 'Hadley' {unparsed}
8587str(roxy_tag(" name" , " Hadley" ))
@@ -113,6 +115,7 @@ is to generate one by parsing a roxygen block with
113115[ ` parse_text() ` ] ( https://roxygen2.r-lib.org/dev/reference/parse_package.md ) :
114116
115117``` r
118+
116119text <- "
117120 #' This is a title
118121 #'
@@ -174,6 +177,7 @@ create a bulleted list of tips about how to use a function. The idea is
174177to take something like this:
175178
176179``` r
180+
177181# ' @tip The mean of a logical vector is the proportion of `TRUE` values.
178182# ' @tip You can compute means of dates and date-times!
179183```
@@ -199,6 +203,7 @@ want to process the text using Markdown so we can just use
199203[ ` tag_markdown() ` ] ( https://roxygen2.r-lib.org/dev/reference/tag_parsers.md ) :
200204
201205``` r
206+
202207roxy_tag_parse.roxy_tag_tip <- function (x ) {
203208 tag_markdown(x )
204209}
@@ -211,6 +216,7 @@ We can check this works by using
211216[ ` parse_text() ` ] ( https://roxygen2.r-lib.org/dev/reference/parse_package.md ) :
212217
213218``` r
219+
214220text <- "
215221 #' Title
216222 #'
@@ -259,6 +265,7 @@ We’re going to create a new custom section called `tip`. It will contain
259265a character vector of tips:
260266
261267``` r
268+
262269roxy_tag_rd.roxy_tag_tip <- function (x , base_path , env ) {
263270 rd_section(" tip" , x $ val )
264271}
@@ -284,6 +291,7 @@ We then need to define a
284291object into text for the ` .Rd ` file:
285292
286293``` r
294+
287295format.rd_section_tip <- function (x , ... ) {
288296 paste0(
289297 " \\ section{Tips and tricks}{\n " ,
@@ -298,6 +306,7 @@ format.rd_section_tip <- function(x, ...) {
298306We can now try this out with ` roclet_text() ` :
299307
300308``` r
309+
301310topic <- roc_proc_text(rd_roclet(), text )[[1 ]]
302311topic $ get_section(" tip" )
303312# > \section{Tips and tricks}{
@@ -339,7 +348,7 @@ for more details.
339348Creating a new roclet is usually a two part process. First, you define
340349new tags that your roclet will work with, unless your roclet only needs
341350information from existing tags, or only needs the path to the package
342- source[ ¹ ] ( #fn1 ) . Second, you define a roclet that tells
351+ source[ ^ 1 ] . Second, you define a roclet that tells
343352[ ` roxygenize() ` ] ( https://roxygen2.r-lib.org/dev/reference/roxygenize.md )
344353what to compute and produce based on this information.
345354
@@ -350,19 +359,22 @@ remember what you’re planning to work on in the future by displaying
350359notes when you document your package. We choose this syntax for ` @memo ` :
351360
352361``` r
362+
353363# ' @memo [Headline] Description
354364```
355365
356366For example:
357367
358368``` r
369+
359370# ' @memo [EFFICIENCY] Currently brute-force; find better algorithm.
360371```
361372
362373As above, we first define a parse method. This time we use custom format
363374based on a regular expression:
364375
365376``` r
377+
366378roxy_tag_parse.roxy_tag_memo <- function (x ) {
367379 if (! grepl(" ^\\ [.*\\ ].*$" , x $ raw )) {
368380 roxy_tag_warning(x , " Invalid memo format" )
@@ -383,6 +395,7 @@ Then we check it works with
383395[ ` parse_text() ` ] ( https://roxygen2.r-lib.org/dev/reference/parse_package.md ) :
384396
385397``` r
398+
386399text <- "
387400 #' @memo [TBI] Remember to implement this!
388401 #' @memo [API] Check best API
@@ -417,7 +430,7 @@ str(block$tags[[1]])
417430```
418431
419432We don’t need a format method because our tag won’t be used to produce
420- Rd sections[ ² ] ( #fn2 ) .
433+ Rd sections[ ^ 2 ] .
421434
422435### The roclet
423436
@@ -426,6 +439,7 @@ Next, we create a constructor for the roclet, which uses
426439` memo ` roclet doesn’t have any options so this is very simple:
427440
428441``` r
442+
429443memo_roclet <- function () {
430444 roclet(" memo" )
431445}
@@ -447,6 +461,7 @@ For this roclet, we’ll have
447461collect all the memo tags into a named list:
448462
449463``` r
464+
450465roclet_process.roclet_memo <- function (x , blocks , env , base_path ) {
451466 results <- list ()
452467
@@ -468,6 +483,7 @@ And then have
468483print them to the screen:
469484
470485``` r
486+
471487roclet_output.roclet_memo <- function (x , results , base_path , ... ) {
472488 for (header in names(results )) {
473489 messages <- results [[header ]]
@@ -483,6 +499,7 @@ Then you can test if it works by using
483499[ ` roc_proc_text() ` ] ( https://roxygen2.r-lib.org/dev/reference/roc_proc_text.md ) :
484500
485501``` r
502+
486503results <- roc_proc_text(
487504 memo_roclet(),
488505 "
@@ -511,6 +528,7 @@ roclet_output(memo_roclet(), results)
511528To use a roclet when developing a package, call
512529
513530``` r
531+
514532roxygen2 :: roxygenize(roclets = " yourPackage::roclet" )
515533```
516534
@@ -534,6 +552,7 @@ package, which doesn’t correspond precisely to any field in the
534552and hence put it in the ` Suggests: ` field:
535553
536554``` r
555+
537556usethis :: use_package(" yourPackage" , type = " Suggests" )
538557```
539558
@@ -555,17 +574,15 @@ parsing features from roxygen2, and does not use
555574[ ` roxygenize() ` ] ( https://roxygen2.r-lib.org/dev/reference/roxygenize.md )
556575at all.
557576
558- ------------------------------------------------------------------------
559-
560- 1 . For example, the no-longer recommended
577+ [ ^ 1 ] : For example, the no-longer recommended
561578 [ ` vignette_roclet() ` ] ( https://roxygen2.r-lib.org/dev/reference/vignette_roclet.md )
562579 only needs the path to the package source as input; it does not use
563580 information from the tag parsing step. Or the {roxylint} package
564581 only uses existing tags; its job is to warn you about suboptimal
565582 roxygen2 style.
566583
567- 2 . Some tags in roxygen2 itself, like ` @importFrom ` , are not meant for
568- the
584+ [ ^ 2 ] : Some tags in roxygen2 itself, like ` @importFrom ` , are not meant
585+ for the
569586 \[ [ ` rd_roclet() ` ] ( https://roxygen2.r-lib.org/dev/reference/rd_roclet.md ) \]
570587 (` @importFrom ` is meant for the
571588 [ ` namespace_roclet() ` ] ( https://roxygen2.r-lib.org/dev/reference/namespace_roclet.md ) ).
0 commit comments