[英]What is the benefit of import in a namespace in R?
The namespace mechanism of R allows one to export
functions which then are visible to the user. R的命名空间机制允许人们
export
随后对用户可见的功能。 Furthermore, it allows to import
functions from other packages. 此外,它允许从其他包
import
功能。 Whereas the benefit of export is obvious, I have more problems understanding the benefit of import. 虽然出口的好处是显而易见的,但我在理解进口的好处方面存在更多问题。
One benefit seems to be, that one can use functions from other packages without attaching the package and thereby saving memory. 似乎有一个好处是,可以使用其他软件包中的函数而无需附加软件包,从而节省内存。 This is exemplified in section 1.6.4 in the writing R extensions manual .
这在写作R扩展手册的1.6.4节中有所例证。
However, there must be other benefits of the import function. 但是,导入功能必须具有其他好处。 Especially, section 1.6.6 (that deals with S4 classes) shows the
namespace
of the stats4 package: 特别是, 第1.6.6节(处理S4类)显示了stats4包的
namespace
:
export(mle)
importFrom("graphics", plot)
importFrom("stats", optim, qchisq)
## For these, we define methods or (AIC, BIC, nobs) an implicit generic:
importFrom("stats", AIC, BIC, coef, confint, logLik, nobs, profile,
update, vcov)
exportClasses(mle, profile.mle, summary.mle)
## All methods for imported generics:
exportMethods(coef, confint, logLik, plot, profile, summary, show, update, vcov)
## implicit generics which do not have any methods here
export(AIC, BIC, nobs)
Here there are functions imported which are neither S4 classes nor generics (where it would make sense to use import as well, as documented in the example in that section ), but functions like plot
from the graphics
package which are automatically loaded when R starts. 这里有进口的函数,它们既不S4类也不泛型(它将使意义使用进口以及,如在记录在例如该部分 ),但功能类似
plot
从graphics
R在时,被自动加载包。
Therefore my question is, what is the benefit of importing functions like plot
, optim
or qchisq
? 因此我的问题是,导入
plot
, optim
或qchisq
等函数有什么好处?
If a function foo
is imported from package Bar then it is found regardless of what the user does to their search path, eg, by attaching a package Baz that also has a function foo
. 如果从包Bar导入函数
foo
,则无论用户对其搜索路径做什么,都可以找到它,例如,通过附加也具有函数foo
的包Baz。 Without a name space, the package code would suddenly find itself using Baz::foo
. 没有名称空间,包代码会突然发现自己使用
Baz::foo
。 There are also efficiency issues ( foo
is found immediately, rather than after searching through all symbols on the search path), but these are likely to be trivial for most applications. 还存在效率问题(
foo
立即被发现,而不是在搜索路径上的所有符号之后),但是对于大多数应用来说这些可能是微不足道的。 In the same way, importFrom
is an improvement over import
because of fewer collisions (or use of unintended functions) and more efficient look-up. 同样,
importFrom
是对import
的改进,因为更少的冲突(或使用非预期的功能)和更有效的查找。
With S4 (and S3) things can get quite complicated. 使用S4(和S3),事情会变得非常复杂。 A non-generic function like
graphics::plot
can be promoted to a generic (with setGeneric
) in two different packages, and each generic can have its own set of methods attached. 可以将一个非泛型函数(如
graphics::plot
提升为两个不同包中的泛型(使用setGeneric
),并且每个泛型都可以附加自己的一组方法。 A package author will want to be precise about which plot
generic, and hence which methods dispatch table, their classes and methods see. 包作者需要准确了解哪个
plot
通用,以及哪些方法调度表,他们的类和方法看到。
Calling a function with pkg::foo
always resolves to the intended function. 使用
pkg::foo
调用函数始终会解析为预期的函数。 It requires that pkg be listed in the Depends: field of the DESCRIPTION file (maybe in Imports: but then it seems like misleading advertising to not import from pkg), polluting the user's search path. 它要求pkg列在DESCRIPTION文件的Depends:字段中(可能在Imports中:但是它似乎误导了广告而不是从pkg导入),污染了用户的搜索路径。 It also involves two symbol look-ups and a function call (
::
), and so is less efficient. 它还涉及两个符号查找和一个函数调用(
::
,因此效率较低。 The lazy and lack-of-attention-to-detail part of me also sees use of ::
as tedious and error prone. 我对懒惰和缺乏关注细节的部分也看到使用
::
作为单调乏味且容易出错。
The package codetoolsBioC (via svn with username and password readonly
) can generate a NAMESPACE file from an existing package (or at least it could before recent changes to R-devel introduced a NAMESPACE on packages without one; I haven't tried codetoolsBioC on such a package). 包codetoolsBioC (通过带有用户名和密码的svn
readonly
)可以从现有的包中生成一个NAMESPACE文件(或者至少可以在R-devel的最近更改之前在没有一个包的情况下在包上引入NAMESPACE;我还没有在这样的包上尝试过codetoolsBioC一袋)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.