Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Roxygen2: How to document a function generated by another function?

Tags:

r

roxygen2

In my package, I have written a functional which takes a function as input and produce another function. How can I document a function produced via such a procedure?

Below is an illustration: use a functional to convert sin() (which takes radian input) to sind() which takes input in degree unit.

rad2deg <- function(f) {
  force(f)
  function(x) f(x / 180 * pi)
}

Here is some documentation for the function below...

sind <- rad2deg(sin)

The rad2deg is just my utility functional used by only me and thus not exported nor documented. I only need to export sind. But I have no idea how it can be done, as it is not even recognized as a function, and it doesn't even have a explicit argument (of course implicitly it takes the argument of the sin function). So, not even the @param tag can be used.

Is there anyone who have any idea?

like image 497
Lawrence Lee Avatar asked May 30 '26 03:05

Lawrence Lee


1 Answers

It works. The following code generates an Rd file and NAMESPACE directive for the sind function.

rad2deg <- function(f) {
  force(f)
  function(x) f(x / 180 * pi)
}

#' sin for degrees
#' @param x an angle in degrees
#' @return sin(x)
#' @export
sind <- rad2deg(sin)
like image 82
TJ Mahr Avatar answered Jun 02 '26 12:06

TJ Mahr