简体   繁体   中英

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)
}

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)
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM