我该如何处理“帮手”? R 包中的函数?

发布于 2024-10-21 05:31:32 字数 406 浏览 2 评论 0原文

背景

我编写了一个 R 包,现在一位合作者(最近刚接触 R 的 CS 毕业生)正在编辑和重构代码。在此过程中,他将我的功能划分为更小、更通用的功能。

他所做的事情是有道理的,但是当我开始使用 package.sculpture() 时,每个函数都有一个文件。现在,他添加了主要函数所依赖的函数,但这在函数本身之外的用途可能有限。

他建议将所有函数放入一个文件中,但我反对,因为当我们处理不同的文件时,更容易进行版本控制。

从那时起,我开始使用 roxygen 来记录文本中的每个函数。

问题

处理函数的推荐方法是什么:显然辅助函数应该与主函数保持一致,但是我需要在多大程度上记录辅助函数?

评论中的 @export 建议很有帮助,但我很好奇其他人如何组织他们的代码。

Background

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.

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.

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

Question

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?

The @export suggestion in the comments is helpful, but I am curious to know how others organize their code.

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

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

发布评论

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

评论(1

同展鸳鸯锦 2024-10-28 05:31:32

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

  1. 当它提高主函数代码的可读性时,和/或
  2. 当它避免复制粘贴代码时,例如,如果在同一函数中多次使用相同的代码。

我确实在主函数的文件中包含了所谓的辅助函数,但前提是这些辅助函数未在任何其他函数中使用。实际上,我认为它们嵌套在主函数中。我确实理解您关于版本控制的论点,但是更改辅助函数归结为更改主函数的性能,因此我认为将它们保留在同一个文件中没有问题。

一些辅助函数可能会在不同的其他函数中使用,然后我将它们保存在自己的文件中。我经常导出这些函数,因为用户可能会对它们感兴趣。将此与 lm 和底层 lm.fit 进行比较,其中高级用户可以充分利用 lm.fit 来加速代码等。

我使用相当多的软件包(并源自 Linux)中使用的命名约定,在每个“隐藏”函数前面加一个点。因此,这使得

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

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

我明确 @export 所有需要导出的函数,而不是辅助函数。判断最终用户是否对某个功能感兴趣并不总是那么容易,但在大多数情况下这是非常清楚的。

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

青年MMV

I cut up my functions under two conditions :

  1. when it improves readibility of the code of the main function, and/or
  2. when it avoids copy-pasting code, eg if the same code is used a couple of times within the same function.

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.

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, ...)
}

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.

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

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