Skip to content
Prev 278228 / 398503 Next

On-demand importing of a package

On Tue, Nov 22, 2011 at 4:27 PM, Martin Morgan <mtmorgan at fhcrc.org> wrote:
[...]
Not just loading, installing a package has a cost, too. Dependencies
are bad, they might make my package fail, and I have no control over
them. It's not just 'Matrix', I have this issue with other packages as
well.

Anyway, 'Imports: Matrix' is just a workaround I think. Or is the
example in my initial mail expected to fail? Why is that? Why can I
call some functions from 'Matrix' that way and why can't I call
others?
It's a matter of opinion, I guess. I find it very annoying when I need
to install a bunch of packages from which I don't use any code, just
because some tiny bit of a package I need uses them. I would like to
spare my users from this.

[...]
Another poster suggested this, that's why I tried. It is clear that I
should not call it directly. All I want to do is having a function
like this:

f <- function() {
 if (require(Matrix)) {
   res <- sparseMatrix(dims=c(5, 5), i=1:5, j=1:5, x=1:5)
 } else {
   res <- diag(1:5)
 }
 y <- rowSums(res)
 res / y
}

Setting the subjective bit, about depending or not, aside, is there
really no solution for this? The code in the manual page examples work
fine without importing the package and just loading it if needed and
available. Why doesn't the code within the package?

Thanks for the patience,
Gabor
[...]