Function commenting conventions in R

回眸只為那壹抹淺笑 提交于 2019-11-28 21:18:27

The canonical way to document your functions and make them accessible to others is to make a package. In order for your package to pass the build checks, you have to supply sufficiently detailed help files for each of your functions / datasets.

Check out http://cran.r-project.org/doc/manuals/R-exts.html#Creating-R-packages

This blog post from Rob J Hyndman was very useful and one of the easiest for me to follow: http://robjhyndman.com/researchtips/building-r-packages-for-windows/

I've started using roxygen to assist in making & compiling packages as of late: http://roxygen.org/

Lots of good resources and people to help when you have questions!

Another (and lower key) alternative you could look into are the comment() and attr() functions to add some meta data to your functions. Here's a quick and silly example:

FOO <- function(x,y) {
 x + y 
}

attr(FOO, "comment") <- "FOO performs simple addition"

#This can be arbitrary. "comment" is special. see ?comment for details.
attr(FOO, "help") <- "FOO expects two numbers, and it will add them together"

You can then see everything associated with FOO by using attributes():

> attributes(FOO)
$source
[1] "function(x,y) {" " x + y "         "}"              

$comment
[1] "FOO performs simple addition"

$help
[1] "FOO expects two numbers, and it will add them together"

Or extract specific parts:

> attr(FOO, "help")
[1] "FOO expects two numbers, and it will add them together"
attr(FOO, "comment")
[1] "FOO performs simple addition"

And in the case of comment, use comment():

> comment(FOO)
[1] "FOO performs simple addition"

In the long term, writing your own package will almost certainly be worth the overhead and time investment, but if for some reason that isn't practical in the short term - here's another option.

You will have to put functions into a package (which makes porting function REALLY easy). I have written a short post about it a while ago with links (I hope they still work) to some relevant documents that expand the subject.

You can generate help files "on the fly" using roxygen, inlinedocs.

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