Documenting function closures

柔情痞子 提交于 2019-12-21 04:11:13

问题


Suppose I have a function closure in my package, for example

f = function(x) {
    x = x
    g = function(y) x <<- y
    h = function() x
    list(g = g, h = h)
}

l = f(5)
l$g(10)
l$h()

What is the correct (in the official CRAN sense) way of documenting this function? In particular,

  1. I would like to use roxygen2
  2. I would like to provide documentation for the functions g and h

回答1:


One way would be to do something similar to ?family where you document g() and h() in the Value section of the .Rd file. Then provide extended documentation about g() and h() in a bespoke \section{foo}, which you point to in the Value section entries for the two functions.

\value{
  Returns an object of class \code{"foo"}, a list with the
  following components:

  \item{g}{function; does foo. See Returned Functions for details.}
  \item{h}{function; does bar. See Returned Functions for details.}
}
\section{Returned Functions}{
  The returned functions do, blah blah...

  \code{g} is defined as

  \preformatted{
  g(x, y, z, ...)
  }

  Arguments are:

  \describe{
    \item{x}{foo}
    \item{y}{foo}
    \item{z}{foo}
  }
}

Whilst roxygen won't be able to do this from the argument @param, but you should be able to write this as an arbitrary roxygen section to add to the Rd file. The Value section can be written as standard roxygen markup, only the bespoke section would need to be entered literally.



来源:https://stackoverflow.com/questions/13027799/documenting-function-closures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!