R:如何使我的包裹使用其他包裹? [英] R: How do I make my package use another package?

查看:119
本文介绍了R:如何使我的包裹使用其他包裹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题. 我正在扩展某人的包裹.当前,它使用软件包A,B,并且它们在DESCRIPTION文件中列出.

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.

如果我需要包C中的函数-将包添加到依赖项中-我是否仅将包添加到Description文件中,这就是所有需要的内容了吗?进入哪个部分-依赖还是依赖?还有其他步骤要执行吗?一旦我的代码需要使用程序包C函数,是否需要使用前缀C :: functionInC()?

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? 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添加到Imports中:使用C函数时,请使用双分号前缀.

Short answer: Add C to Imports: and when using the C functions, use the double semicolon prefix.

更长的上下文:

下面的链接提供以下建议 http://r-pkgs.had.co.nz/namespace.html#imports

The link below provides the following advice http://r-pkgs.had.co.nz/namespace.html#imports

R函数

如果您仅使用另一个软件包中的几个函数,我的建议是记下Description文件的Imports:字段中的软件包名称,并使用::显式调用函数,例如pkg :: fun ().

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 ::, e.g., pkg::fun().

如果您反复使用函数,则可以通过使用@importFrom pgk fun导入函数来避免::.这对性能也没有什么好处,因为:: ::会使函数评估时间增加大约5 µs.

If you are using functions repeatedly, you can avoid :: by importing the function with @importFrom pgk fun. This also has a small performance benefit, because :: adds approximately 5 µs to function evaluation time.

或者,如果您重复使用另一个程序包中的许多功能,则可以使用@import程序包将其全部导入.这是最不推荐的解决方案,因为它会使您的代码难以阅读(您无法分辨函数的来源),并且如果您@import许多包,则会增加函数名冲突的可能性.

Alternatively, if you are repeatedly using many functions from another package, you can import all of them using @import package. 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.

这篇关于R:如何使我的包裹使用其他包裹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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