我应该如何处理 R 包中的“助手"功能?

编程入门 行业动态 更新时间:2024-10-17 14:31:04
本文介绍了我应该如何处理 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.

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

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 建议很有帮助,但我很想知道其他人如何组织他们的代码.

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

推荐答案

我在两种情况下切割了我的功能:

I cut up my functions under two conditions :

  • 当它提高主函数代码的可读性时,和/或
  • 当它避免复制粘贴代码时,例如,如果在同一个函数中多次使用相同的代码.
  • 我确实在主函数的文件中包含了所谓的辅助函数,但前提是这些辅助函数不用于任何其他函数.实际上,我认为它们嵌套在 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.

    我使用相当多的包(并从 linux 派生)中使用的命名约定,通过在每个隐藏"函数前面加上一个点.所以这使得

    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 包中的“助手"功能?

    本文发布于:2023-10-18 11:58:29,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1504169.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:如何处理   包中   助手   功能   quot

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!