R 中的函数注释约定

发布于 2024-11-14 23:56:59 字数 200 浏览 1 评论 0原文

我对 R 相当陌生,我一直在脚本文件中定义一些我自己的函数。我打算让其他人稍后重用它们,但我找不到任何有关 R 函数注释约定的指南。有什么方法可以让 help("my_function_name") 显示一些帮助吗?如果没有,我是否只需在脚本文件中记录该函数,以便有人必须打印出(或打开脚本的源代码)才能查看注释?

谢谢,

哈米

I'm fairly new to R, and I have been defining some of my own functions in script files. I'm intending for others to re-use them later, and I can't find any guides on R function commenting conventions. Is there any way for me to make help("my_function_name") show some help? If not, do I just document the function in the script file, so that someone has to print out (or open the source of) a script to see the comments?

Thanks,

Hamy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痴情换悲伤 2024-11-21 23:56:59

于 2019 年 12 月更新此问题,因为 R 宇宙自 2011 年最初编写以来已发生变化

我推荐的资源现在是 http://r-pkgs.had.co.nz/

原始答案(链接大多已过时)

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

查看 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/

有很多好的资源和人员可以在您有帮助时提供帮助问题!

Updating this question December 2019 as the R-universe has changed since 2011 when originally written

My recommended resource is now http://r-pkgs.had.co.nz/

Original answer (links are mostly out of date)

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!

小嗷兮 2024-11-21 23:56:59

您可以考虑的另一个(也是更低调的)替代方案是 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"

然后,您可以使用 attributes() 查看与 FOO 相关的所有内容:

> 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"

或者提取特定部分:

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

如果是注释,请使用 comment()

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

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

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.

北方的巷 2024-11-21 23:56:59

您必须将函数放入包中(这使得移植函数非常容易)。我写了一个 短文,其中包含一些扩展该主题的相关文档的链接(我希望它们仍然有效)。

您可以使用 roxygen“即时”生成帮助文件, inlinedocs

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.

鸩远一方 2024-11-21 23:56:59

如果您不构建包,则可以使用 docstring 包,该包提供与 Python 文档字符串类似的功能。

library(docstring)

square <- function(x){

    #' Square a number
    #'
    #' Calculates the square of the input
    #'
    #' @param x the input to be squared

    return(x^2)
}

您可以通过调用?square来查看评论。
输入图片此处描述
输出

本教程可能会有所帮助。

If you are not building a package, you can use docstring package, which offers similar functions as Python's docstring.

library(docstring)

square <- function(x){

    #' Square a number
    #'
    #' Calculates the square of the input
    #'
    #' @param x the input to be squared

    return(x^2)
}

You can see the comments by calling ?square.
enter image description here
output

This tutorial might be helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文