简体   繁体   English

未附加名称空间时的R包依赖性问题

[英]R package dependency issues when namespace is not attached

I have always assumed that having a package in the 'Depends' field would automatically also import the namespace. 我一直认为在'Depends'字段中有一个包会自动导入命名空间。 However, it seems that in R 2.15, dependencies do not become available until the package is actually attached to the searchpath. 但是,似乎在R 2.15中,在包实际附加到搜索路径之前,依赖性不可用。 Is this intended behavior? 这是预期的行为吗?

The problem appears as follows: Suppose there is a package 'Child' which Depends , but does not explicitly Import a package called 'Parent' and contains a function that calls out to an object in the namespace of 'Parent'. 问题如下所示:假设有一个包'Child' 取决于 ,但没有显式导入名为'Parent'的包,并包含一个调用'Parent'命名空间中的对象的函数。 Then when this function is called without actually attaching 'Child', the function in 'Parent' cannot be found. 然后,在没有实际附加“Child”的情况下调用此函数时,无法找到“Parent”中的函数。

Here an example from the manual of the bigdata package, but the problem is very widespread: 这里是bigdata包手册中的一个例子,但问题非常普遍:

x = matrix(rnorm(50*80),50,80)
beta = c(3,2,1.5,rep(0,77))
y = rnorm(50) + x%*%beta
z1 = bigdata::lasso.stars(x,y)

The example fails because lasso.stars depends on 'glmnet' which is not loaded until bigdata is attached. 该示例失败,因为lasso.stars依赖于'glmnet',直到附加bigdata才会加载。 The only way to be able to call lasso.stars is to actually attach the bigdata package: 能够调用lasso.stars的唯一方法是实际附加bigdata包:

library(bigdata)
z1 = bigdata::lasso.stars(x,y)

Now to further complicate things, it seems that this problem is inherited to any 'grandchild' package that Imports in this case the lasso.stars function. 现在为了进一步复杂的事情,看来这个问题被继承到任何“孙子”包,在这种情况下, 进口 lasso.stars功能。 I have a hard time finding a good example but I am sure they are out there. 我很难找到一个很好的例子,但我相信他们在那里。

Is this a bug? 这是一个错误吗? I know that it can be avoided by asking package authors to use Imports instead of Depends, but in practice the majority of the packages on CRAN still use Depends. 我知道可以通过要求包作者使用Imports而不是Depends来避免它,但实际上CRAN上的大多数包仍然使用Depends。 It seems like the problem is easily avoided if R would automatically import the namespace of any Depends packages into to the child package namespace. 如果R会自动将任何Depends包的命名空间导入子包命名空间,似乎很容易避免这个问题。

对于那些感兴趣的人, 这里继续讨论r-devel邮件列表:

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

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