问题描述
当我进行任何复杂的R项目时,我的脚本很快就会变得冗长而混乱.
When I undertake an R project of any complexity, my scripts quickly get long and confusing.
我可以采用哪些实践来使我的代码始终令人愉快?我在考虑类似的东西
What are some practices I can adopt so that my code will always be a pleasure to work with? I'm thinking about things like
- 函数在源文件中的位置
- 何时将某些内容分解到另一个源文件中
- 主文件中应该包含什么
- 使用职能作为组织单位(鉴于R使其难以访问全局状态,这是否值得)
- 缩进/换行惯例.
- 治疗(如{?
- 将诸如}}之类的内容放在1或2行上?
- Placement of functions in source files
- When to break something out to another source file
- What should be in the master file
- Using functions as organizational units (whether this is worthwhile given that R makes it hard to access global state)
- Indentation / line break practices.
- Treat ( like {?
- Put things like )} on 1 or 2 lines?
基本上,组织大型R脚本的经验法则是什么?
Basically, what are your rules of thumb for organizing large R scripts?
推荐答案
标准答案是使用软件包-请参见编写R扩展手册以及网络上的其他教程.
The standard answer is to use packages -- see the Writing R Extensions manual as well as different tutorials on the web.
它给你
- 一种按主题组织代码的准自动方式
- 强烈鼓励您编写帮助文件,让您考虑一下界面
- 通过
R CMD check
进行的健全性检查 - 添加回归测试的机会
- 以及命名空间的一种方法.
- a quasi-automatic way to organize your code by topic
- strongly encourages you to write a help file, making you think about the interface
- a lot of sanity checks via
R CMD check
- a chance to add regression tests
- as well as a means for namespaces.
只需在代码上运行
source()
即可处理非常短的代码段.其他所有内容都应放在包中-即使您不打算发布它,因为您可以为内部存储库编写内部包.Just running
source()
over code works for really short snippets. Everything else should be in a package -- even if you do not plan to publish it as you can write internal packages for internal repositories.对于如何编辑"部分, R Internals 手册在第6节中具有出色的 R编码标准.否则,我倾向于在 Emacs的ESS模式.
As for the 'how to edit' part, the R Internals manual has excellent R coding standards in Section 6. Otherwise, I tend to use defaults in Emacs' ESS mode.
更新2008年8月13日: David Smith刚刚写了关于 Google R样式指南.
Update 2008-Aug-13: David Smith just blogged about the Google R Style Guide.
这篇关于如何组织大型R程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!