R package dependency issues when namespace is not attached
Asked Answered
V

1

11

I have always assumed that having a package in the 'Depends' field would automatically also import the namespace. However, it seems that in R 2.15, dependencies do not become available until the package is actually attached to the searchpath. 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'. Then when this function is called without actually attaching 'Child', the function in 'Parent' cannot be found.

Here an example from the manual of the bigdata package, but the problem is very widespread:

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. The only way to be able to call lasso.stars is to actually attach the bigdata package:

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. 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. 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.

Vannoy answered 11/5, 2012 at 22:21 Comment(7)
+1 Great question. It's one I've thought of broaching myself, but have put off, thinking it might be better raised over on R-devel (since I think the R-core members are the only folks who can solve this). They must have discussed it, but I've searched quickly several times and have seen no mention of it, which seems odd...Encephaloma
@Jeroen, could you please provide the example? When I try library(xts), then the export functions of zoo is automatically loaded.Edina
@Edina the example is in the topic. The xts package has a NAMESPACE file in which it manually imports required functions form zoo. Therefore it doesn't have the problem I think.Vannoy
Do you talk about random package? But I cannot find the depends of bigdata in the random package.Edina
I changed the wording a bit. Just run the 4 lines in a clean R session and it will fail.Vannoy
I imagine this is not a bug. Why not library(bigdata)? Is there any reason?Edina
@Edina -- The problem is that if you write a package that needs a function like lasso.stars() from the bigdata package, you can't get away with doing that via an Imports directive. If you just put import(bigdata) in the NAMESPACE file, the package will find lasso.stars(), but won't be able to use it, because it won't find glmnet() from the glmnet package. So, as a package writer, you are forced to instead use Depends: bigdata (which causes both bigdata and glmnet to be attached to the search path).Encephaloma
V
2

For those that are interested, the discussion continues here on the r-devel mailing list:

Vannoy answered 13/5, 2012 at 21:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.