我应该如何处理R包中的“帮助”函数? [英] How should I handle 'helper' functions in an R package?

查看:198
本文介绍了我应该如何处理R包中的“帮助”函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个R程序包,现在一个协作者(最近刚接触R的CS研究员)正在编辑和重构代码。在这个过程中,他把我的功能分成更小,更通用的功能。

I written an R package, and now a collaborator (recent CS grad who is new to R) is editing and refactoring the code. In the process, he is dividing up my functions into smaller, more generic functions.

他在做什么是有意义的,但是当我开始使用 package.skeleton()每个函数。现在,他已经添加了主要功能所依赖的功能,但在功能本身之外可能只有有限的使用。

What he is doing makes sense, but when I started with package.skeleton(), I had one file per function. Now, he has added functions on which the primary function depends, but that may have limited use outside the function itself.

他建议所有的功能都进入一个文件,但是我反对,因为当我们处理不同的文件时,更容易做版本控制。

He suggests that all the functions go into a single file, but I am against that because it is easier to do version control when we work on different files.

我已经开始使用roxygen记录文本中的每个函数。

I have since started using roxygen to document each function within the text.

建议的处理函数的方式:清楚的帮助函数应该留在main函数,但在什么程度上需要记录帮助函数?

What is the recommended way to handle functions: clearly the helper functions should stay with the the main function, but to what extent do I need to document helper functions?

注释中的 @export

推荐答案

我在两个条件下剪切我的函数:

I cut up my functions under two conditions :


  1. ,和/或

  2. 例如,如果相同的代码在同一个函数中使用了几次。

我包括所谓的辅助函数在主函数的文件中,但只要这些辅助函数不在任何其他函数中使用。实际上,我认为它们嵌套在main函数中。我理解你的版本控制的参数,但更改辅助函数是为了改变主要功能的性能,所以我认为没有问题,保持在同一个文件。

I do include the so-called helper functions in the file of the main function, but only as long as those helper functions are not used in any other function. Actually, I consider them nested within the main function. I do understand your argument for version control, but changing the helper function comes down to changing the performance of the main function, so I see no problem in keeping them in the same file.

一些帮助函数可能用于不同的其他函数,然后我将它们保存在自己的文件中。通常我会导出这些功能,因为他们可能是用户感兴趣。与 lm 和底层 lm.fit 比较,高级用户可以使用 lm.fit 可以加速代码等。

Some helper functions might be used in different other functions, and then I save them in their own file. Often I do export those functions, as they might be of interest for the user. Compare this to eg lm and the underlying lm.fit, where advanced users could make decent use of lm.fit for speeding up code etc.

我使用的命名约定在很多软件包通过一个点前面的每个隐藏功能。这样,

I use the naming convention used in quite some packages (and derived from linux), by preceding every "hidden" function by a dot. So that makes

.helper.function <- function(x, ...){
    ... some code ...
}

main.function <- function(x, ...){
    ...some code, including .helper.function(y, ...)
}

我显式@export所有需要导出的函数,从不帮助函数。判断一个函数是否可能对最终用户感兴趣并不总是容易,但在大多数情况下它很清楚。

I explicitly @export all functions that need exporting, never the helper functions. It's not always easy to judge whether a function might be of interest to an end user, but in most cases it's pretty clear.

举一个例子:几行代码切断NA行我认为一个帮助函数。一个更复杂的函数,用于将数据集转换为正确的格式,并导出和归档。

To give an example : A few lines of code to cut off NA lines I consider a helper function. A more complex function to convert the dataset to the correct format I export and document.

YMMV

这篇关于我应该如何处理R包中的“帮助”函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆