How to run documentation functions inside roxygen2 @examples?

I have numerous similar functions where only one or two words need to change in the documentation. To limit duplication, I'm using functions inside the documentation.

This works in roxygen2 for @description and @param, but I cannot get it to work for @examples. The inline code is not evaluated.

Example

useless_description <- function(name) {
  paste0('This useless function prints the word "', name , '" n times.')
}

useless_example <- function(name) {
  paste("# Print ", name, " 1 time",
        name, "(1)",
        "",
        "# Print ", name, " 3 times",
        name, "(3)",
        sep = "\n"
        )
}

#' @title Hello
#' @description `r useless_description("hello")`
#' @examples `r useless_example("hello")`
hello <- function(n) {
  print(rep("hello", n))
}

#' @title Goodbye
#' @description `r useless_description("goodbye")`
#' @examples `r useless_example("hello")`
goodbye <- function(n) {
  print(rep("goodbye", n))
}

When running devtools::document(), this is the help page that is created for the function hello():

image

Possible solution using @eval

I have found a way around this, but it uses the superseded keyword @eval.

useless_example2 <- function(name) {
  c("@examples ",
    paste0("# Print ", name, " 1 time"),
    paste0(name, "(1)"),
    "",
    paste0("# Print ", name, " 3 time"),
    paste0(name, "(3)")
    )
}

#' @title Hello
#' @description `r useless_description("hello")`
#' @eval useless_example2("hello")
hello <- function(n) {
  print(rep("hello", n))
}

In the help page the code is now evaluated properly. See image of help page on imgur.

Question

Is there a way to evaluate inline code in roxygen2 @examples without using superseded functions?

(I have also asked this question on Stackoverflow.)

This questions has now been answered on Stackoverflow.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.