rroxygen2

How do I use a variable's value inside roxygen2 documentation?


I have a function with an argument that can only take certain values. I want to list the possible values in the documentation.

#' Example function
#' 
#' @param x Character, possible values are "a", "b", and "c".
foo <- function(x = c("a", "b", "c")) {
  x <- match.arg(x)
}

I would like to only define the list once, and then use it in both in the documentation and the body of the function, so I only have to write it once.

Here's an outline that doesn't work, but shows the idea.

x_values <- c("a", "b", "c")
#' Example that doesn't work
#' 
#' @param x Character, possible values are {x_values}.
foo <- function(x = x_values) {
  x <- match.arg(x)
}

Solution

  • You can put your code in backticks starting with r.

    #' @param x Character, possible values are `r toString(x_values)`.
    foo <- function(x = x_values) {
      x <- match.arg(x)
    }
    

    be sure to have

    Roxygen: list(markdown = TRUE)
    

    in your DESCRPTION

    See: https://cran.r-project.org/web/packages/roxygen2/vignettes/rd-formatting.html