R 中的函数注释约定
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
于 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!
您可以考虑的另一个(也是更低调的)替代方案是
comment()
和attr()
函数,用于向函数添加一些元数据。这是一个简单而愚蠢的示例:然后,您可以使用
attributes()
查看与FOO
相关的所有内容:或者提取特定部分:
如果是注释,请使用
comment()
:从长远来看,编写自己的包几乎肯定是值得的开销和时间投资,但如果由于某种原因在短期内不切实际 - 这是另一种选择。
Another (and lower key) alternative you could look into are the
comment()
andattr()
functions to add some meta data to your functions. Here's a quick and silly example:You can then see everything associated with
FOO
by usingattributes()
:Or extract specific parts:
And in the case of comment, use
comment()
: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.
您必须将函数放入包中(这使得移植函数非常容易)。我写了一个 短文,其中包含一些扩展该主题的相关文档的链接(我希望它们仍然有效)。
您可以使用 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.
如果您不构建包,则可以使用
docstring
包,该包提供与 Python 文档字符串类似的功能。您可以通过调用
?square
来查看评论。输出
本教程可能会有所帮助。
If you are not building a package, you can use
docstring
package, which offers similar functions as Python's docstring.You can see the comments by calling
?square
.output
This tutorial might be helpful.