简体   繁体   English

R:如何让我的包使用另一个包?

[英]R: How do I make my package use another package?

This is very simple question.这是一个非常简单的问题。 I am extending someone's package.我正在扩展某人的包裹。 It currently uses packages A, B and they are listed in the DESCRIPTION file.它当前使用包 A、B,它们列在描述文件中。

If I need functions from package C - to add a package to the dependencies - do I just add the package in the DESCRIPTION file and that is all that is needed?如果我需要包 C 中的函数 - 将包添加到依赖项中 - 我是否只需将包添加到说明文件中就可以了? Into what section - Depends or Imports?进入哪个部分 - 依赖还是进口? Are there more other steps to make?还有其他步骤吗? Do I need to use prefix C::functionInC() once my code needs to use a package C function?一旦我的代码需要使用包 C 函数,我是否需要使用前缀 C::functionInC()?

Short answer: Add C to Imports: and when using the C functions, use the double semicolon prefix.简短回答:将 C 添加到导入:并且在使用 C 函数时,使用双分号前缀。

Longer context:更长的上下文:

The link below provides the following advice http://r-pkgs.had.co.nz/namespace.html#imports下面的链接提供了以下建议http://r-pkgs.had.co.nz/namespace.html#imports

R functions R 函数

If you are using just a few functions from another package, my recommendation is to note the package name in the Imports: field of the DESCRIPTION file and call the function(s) explicitly using ::, eg, pkg::fun().如果您只使用另一个包中的几个函数,我的建议是记下描述文件的 Imports: 字段中的包名称,并使用 :: 显式调用该函数,例如 pkg::fun()。

If you are using functions repeatedly, you can avoid :: by importing the function with @importFrom pgk fun.如果您重复使用函数,则可以通过使用 @importFrom pgk fun 导入函数来避免 ::。 This also has a small performance benefit, because :: adds approximately 5 µs to function evaluation time.这也有一个小的性能优势,因为 :: 增加了大约 5 µs 的函数评估时间。

Alternatively, if you are repeatedly using many functions from another package, you can import all of them using @import package.或者,如果您重复使用另一个包中的许多函数,则可以使用 @import 包导入所有这些函数。 This is the least recommended solution because it makes your code harder to read (you can't tell where a function is coming from), and if you @import many packages, it increases the chance of conflicting function names.这是最不推荐的解决方案,因为它使您的代码更难阅读(您无法分辨函数来自何处),并且如果您 @import 多个包,则会增加函数名称冲突的可能性。

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

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