Basics

Roxygen is extensible with user-defined roclets. It means that you can take advantage of Roxygen’s parser and extend it with your own @tags.

There are two primary ways to extend roxygen2:

  • Add new tag that generates a new top-level section in .Rd files.

  • Add a new roclet that does anything you like.

This vignette will introduce you to the key data structures in roxygen2, and then show you how to use these two extension points. This vignette is very rough, so you should expect to have to also read some roxygen2 source code to understand all the extension points. Hopefully it’s useful enough to help you get started, and if you have problems, please file an issue!

library(roxygen2)

Key data structures

Before we talk about extending roxygen2, we need to first discuss two important data structures that power roxygen: tags and blocks.

Tags

A tag (a list with S3 class roxy_tag) represents a single tag. It has the following fields:

  • tag: the name of the tag.

  • raw: the raw contents of the tag (i.e. everything from the end of this tag to the beginning of the next).

  • val: the parsed value, which we’ll come back to shortly.

  • file and line: the location of the tag in the package. Used with roxy_tag_warning() to produce informative error messages.

You can construct tag objects by hand with roxy_tag():

However, you should rarely need to do so, because you’ll typically have them given to you in a block object, as you’ll see shortly.

Blocks

A block (a list with S3 class roxy_block) represents a single roxygen block. It has the following fields:

  • tags: a list of roxy_tags.
  • call: the R code associated with the block (usually a function call).
  • file and line: the location of the R code.
  • object: the evaluated R object associated with the code.

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

Adding a new .Rd tag

The easiest way to extend roxygen2 is to create a new tag that adds output to .Rd files. This requires two steps:

  1. Define a roxy_tag_parse() method that describes how to parse our new tag.

  2. Define a roxy_tag_rd() method that describes how to convert the tag into .Rd commands.

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. The idea is to take something like this:

And generate Rd like this:

The first step is to define a method for roxy_tag_parse() that describes how to turn the parse the tag text. The name of the class will be roxy_tag_{tag}, which in this case is roxy_tag_tip. This function takes a roxy_tag as input, and it’s job is to set x$val to a convenient parsed value that will be used later by the roclet. Here we want to process the text using markdown so we can just use tag_markdown():

We check this works by using parse_text():

(Here I explicit turn markdown parsing on using @md; it’s usually turned on for a package using roxygen options).

Next we define a method for roxy_tag_rd(), which must create an rd_section(). We’re going to create a new section called tip. It will contain a character vector of tips:

This additional layer is needed because there can multiple tags of the same time in a single block, and multiple blocks can contribute to the same .Rd file. The job of the rd_section is to combine all the tags into a single top-level Rd section. Each tag generates an rd_section which is then combined with any previous section using merge(). The default merge.rd_section() just concatenates the values together (rd_section(x$type, c(x$value, y$value))); you can override this method if you need more sophisticated behaviour.

We then need to define a format() method to converts this object into text for the .Rd file:

We can now try this out with roclet_text():

Note that there is no namespacing so if you’re defining multiple new tags I recommend using your package name as common prefix.

Creating a new roclet

Creating a new roclet is usually a two part process. First, you define new tags that your roclet will work with. Second, you define a roclet that tells roxygen how to process an entire package.

The roclet

Next we create a constructor for the roclet, which uses roclet(). Our memo roclet doesn’t have any options so this is very simple:

To give the roclet behaviour, you need to define method. There are two methods that almost every roclet will use:

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

And then have roclet_output() just print them to the screen:

Then you can test it works by using roc_proc_text():