R中的函数注释约定

Ham*_*amy 20 r commenting conventions

我对R很新,我一直在脚本文件中定义一些自己的函数.我打算让其他人以后重新使用它们,我找不到任何关于R函数注释约定的指南.我有什么方法可以help("my_function_name")帮助我吗?如果没有,我是否只记录脚本文件中的函数,以便有人打印出(或打开源代码)脚本来查看注释?

谢谢,

Hamy

Cha*_*ase 21

记录您的功能并让其他人可以访问的规范方法是制作一个包.为了使您的包能够通过构建检查,您必须为每个功能/数据集提供足够详细的帮助文件.

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

这篇来自Rob J Hyndman的博客文章非常有用,也是我最容易理解的文章之一:http://robjhyndman.com/researchtips/building-r-packages-for-windows/

我已经开始使用roxygen来帮助制作和编译软件包:http://roxygen.org/

当你有疑问时,有很多好的资源和人帮忙!


Cha*_*ase 15

另一个(,下键),替代你可以看看是comment()attr()功能的一些元数据添加到您的功能.这是一个快速而愚蠢的例子:

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"
Run Code Online (Sandbox Code Playgroud)

然后,您可以FOO使用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"
Run Code Online (Sandbox Code Playgroud)

或提取特定部分:

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

在评论的情况下,使用comment():

> comment(FOO)
[1] "FOO performs simple addition"
Run Code Online (Sandbox Code Playgroud)

从长远来看,编写自己的软件包几乎肯定值得花费开销和时间,但如果由于某种原因在短期内不切实际 - 这是另一种选择.


Rom*_*rik 7

您必须将函数放入包中(这使得移植功能非常简单).我刚才写了一篇关于它的简短帖子,链接(我希望它们仍然可以工作)到一些扩展主题的相关文档.

您可以使用roxygen,inlinedocs " 动态 "生成帮助文件.