Search code examples
rdevtoolsroxygen2

verbatim rendering of strings with { } in code chunks of roxygen documentation


I have some regex expressions that I want to put into my R package documentation:

this <- c("letter-number"="^([a-zA-Z]{1}[0-9]+)")

When I put this into the roxygen doc strings as

#' @details This is the regex schema that I use: "`r this`".
#' @export
this <- c("letter-number"="^([a-zA-Z]{1}[0-9]+)")

it gets rendered as ^([a-zA-Z]10-9+), i.e. the curly brackets are absorbed / the expression inside the curly brackets is evaluated. Looking at the compiled .Rd file, I suspect it is the problem of the help file rendering, as in that file, the line shows up as "^([a-zA-Z]{1}\link{0-9}+)" (although it is problematic that roxygen has converted the square brackets to the \link{} instruction; it gets ignored when I view the help file with ?this).

What is an appropriate way to display this kind of string verbatim?

> packageVersion("roxygen2")
[1] ‘7.1.2’
> packageVersion("devtools")
[1] ‘2.4.3’
> packageVersion("glue")
[1] ‘1.6.2’

(I am stuck with a dated R 4.1.2 at the moment.)


Solution

  • While this might be a bug in roxygen2 (or its use of rmarkdown), if you can tolerate a helper-function, then we can do this:

    backticks <- function(z) paste0('`', z, '`')
    #' This.
    #' @details This is the regex schema that I use: "`r backticks(this)`".
    #' @export
    this <- c("letter-number"="^([a-zA-Z]{1}[0-9]+)")
    

    Rendered with devtools::document() (among other ways, same-effect) results in this in the man/this.Rd file:

    \details{
    This is the regex schema that I use: "\verb{^([a-zA-Z]\{1\}[0-9]+)}".
    }
    

    which renders as

    Details:
    
         This is the regex schema that I use: "^([a-zA-Z]{1}[0-9]+)".
    

    For context, R-4.2.3 in emacs/ESS with

    packageVersion("roxygen2")
    # [1] '7.2.3'
    packageVersion("devtools")
    # [1] '2.4.5'
    packageVersion("rmarkdown")
    # [1] '2.20'
    packageVersion("glue")
    # [1] '1.6.2'