简体   繁体   English

我应该如何引用导入包中的函数?

[英]How should I reference functions in imported packages?

When creating an R package, there are at least two alternatives for referencing functions in imported packages. 创建R包时,至少有两种方法可用于引入导入包中的函数。

Either, 要么,

  1. Explicitly name the function using the double colon operator whenever you call it, package::function . 每次调用它时,使用双冒号运算符明确命名函数, package::function

  2. Add importFrom(package, function) to the NAMESPACE file, either directly or via an #' @importFrom package function roxygen tag. importFrom(package, function)直接添加到NAMESPACE文件中,或通过#' @importFrom package function roxygen标记添加。

What are the advantages and disadvantages of each method? 每种方法有哪些优缺点?

Are there any technical differences in what each syntax achieves? 每种语法实现的技术差异是否存在?

Arguments in favour of using package::function 支持使用package::function

It makes it completely clear where the function has come from. 它完全清楚了功能的来源。

Arguments in favour of using @importFrom package function 支持使用@importFrom package function

It involves less typing, particularly when a function is used many times by your package. 它涉及较少的打字,特别是当您的包使用多次功能时。

Since it involves looking up the package and a call to the :: function, package::function has a small runtime performance penalty. 由于它涉及查找包和调用:: function,因此package::function的运行时性能损失很小。 See https://stackoverflow.com/a/7283511/134830 . 请参阅https://stackoverflow.com/a/7283511/134830

On balance, what's the verdict? 总的来说,判决是什么?

Both methods do the job and arguments either way aren't overwhelming, so don't lose sleep over this. 这两种方法都可以完成这项工作,并且无论是哪种方式都不是压倒性的,所以不要为此而失眠。 Just pick one method and stick to it. 只需选择一种方法并坚持下去。

The policy that has been adopted at my place of work is that for a few commonly used packages, @importFrom roxygen tags should be used. 在我的工作地点采用的政策是,对于一些常用的包,应使用@importFrom roxygen标签。 For example, developers are expected to know that ddply comes from plyr , or functions beginning str_ come from stringr . 例如,开发人员应该知道ddply来自plyr ,或者起始str_函数来自stringr In this case, the explicit parentage of the function isn't as useful to know. 在这种情况下,函数的显式父母对于知道并不那么有用。 For functions outside this core list, (or if there is any ambiguity) :: should be used to make it clear where it came from. 对于这个核心列表之外的函数,(或者如果有任何歧义) ::应该用来清楚它来自哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM