[R] On-demand importing of a package

William Dunlap wdunlap at tibco.com
Tue Nov 22 22:34:07 CET 2011


I also wondered why it is important to not mention
Matrix in the DEPSCRIPTION file's Depends or Imports
lines, even though some functions in the package
require it.  If this is a hard requirement you could
split your package into two packages, pkgBasic and
pkgEnhanced.  pkgBasic would not not depend on Matrix
and pkgEnhanced would depend on Matrix and pkgBasic.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Martin Morgan
> Sent: Tuesday, November 22, 2011 1:27 PM
> To: Gábor Csárdi
> Cc: R mailing list
> Subject: Re: [R] On-demand importing of a package
> 
> On 11/22/2011 01:16 PM, Gábor Csárdi wrote:
> > Hi Josh,
> >
> > On Tue, Nov 22, 2011 at 3:31 PM, Joshua Wiley<jwiley.psych at gmail.com>  wrote:
> >> Hi Gábor,
> >>
> >> You could import rowSums.  This will not fully attach Matrix.  I am
> >> not sure there is a really good solution for what you want to do.  To
> >> fully use and validate your package, Matrix appears to be required.
> >> This is different from simply, for example, enhancing the Matrix
> >> package.
> >
> > importing rowSums() from NAMESPACE requires having Matrix in the
> > 'Depends:' line, I think. I would like to avoid that, if possible.
> 
> No need to Depend:. Use
> 
> Imports: Matrix
> 
> plus in the NAMESPACE file
> 
>   importFrom(Matrix, rowSums)
> 
> Why do you not want to do this? Matrix is available for everyone,
> Imports: doesn't influence the package search path. There is a cost
> associated with loading the library in the first place, but...?
> 
> >
> > Sure, to validate my package it is required. That is fine. For "fully"
> > using it might be required, but most users don't fully use a package,
> > they just use 1-20-50% of the functionality, depending on the size of
> > the package. My package does not depend on Matrix, except in less than
> > 1% of its (quite many) functions.
> 
> I'm more into black-and-white -- it either needs Matrix or not;
> apparently it does.
> 
> >
> >> You could just write the functions assuming matrix is there, make sure
> >> the examples are marked don't run, and tell users if they want to use
> >> them, they need to load Matrix first.  I do this with OpenMx in my
> >> package.
> >
> > Hmmm, this is close to what I want to do. Actually my functions work,
> > even if Matrix is not there, but they work better with Matrix. The
> > problem is that if I do this, then I get the error I've shown in my
> > initial email. I.e. Matrix is there, it is loaded, but the rowSums()
> > generic is not called for some reasons.
> 
> In another message you mention
> 
>  > Matrix:::rowSums(W)
> Error in callGeneric() :
>    'callGeneric' must be called from a generic function or method
> 
> but something else is going on -- you don't get to call methods
> directly; you're getting Matrix::rowSums (it's exported, so no need for
> a :::, see getNamespaceExports("Matrix")). Maybe traceback() after the
> error would be insightful?
> 
> Martin
> 
> >
> > Thanks again,
> > Gabor
> >
> >> Cheers,
> >>
> >> Josh
> >>
> >> On Tue, Nov 22, 2011 at 12:16 PM, Gábor Csárdi<csardi at rmki.kfki.hu>  wrote:
> >>> Dear All,
> >>>
> >>> in some functions of my package, I use the Matrix S4 class, as defined
> >>> in the Matrix package.
> >>>
> >>> I don't want to depend on Matrix, however, because my package is
> >>> perfectly fine without Matrix, most of the functionality does not need
> >>> Matrix. Matrix is so included in the 'Suggests' line.
> >>>
> >>> I load Matrix via require(), from the functions that really need it.
> >>> This mostly works fine, but I have an issue now that I cannot sort
> >>> out.
> >>>
> >>> If I define a function like this in my package:
> >>>
> >>> f<- function() {
> >>>   require(Matrix)
> >>>   res<- sparseMatrix(dims=c(5, 5), i=1:5, j=1:5, x=1:5)
> >>>   y<- rowSums(res)
> >>>   res / y
> >>> }
> >>>
> >>> then calling it from the R prompt I get
> >>> Error in rowSums(res) : 'x' must be an array of at least two dimensions
> >>>
> >>> which basically means that the rowSums() in the base package is
> >>> called, not the S4 generic in the Matrix package. Why is that?
> >>> Is there any way to work around this problem, without depending on Matrix?
> >>>
> >>> I am doing this on R 2.14.0, x86_64-apple-darwin9.8.0.
> >>>
> >>> Thank You, Best Regards,
> >>> Gabor
> >>>
> >>> --
> >>> Gabor Csardi<csardi at rmki.kfki.hu>      MTA KFKI RMKI
> >>>
> >>> ______________________________________________
> >>> R-help at r-project.org mailing list
> >>> https://stat.ethz.ch/mailman/listinfo/r-help
> >>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> >>> and provide commented, minimal, self-contained, reproducible code.
> >>>
> >>
> >>
> >>
> >> --
> >> Joshua Wiley
> >> Ph.D. Student, Health Psychology
> >> Programmer Analyst II, ATS Statistical Consulting Group
> >> University of California, Los Angeles
> >> https://joshuawiley.com/
> >>
> >
> >
> >
> 
> 
> --
> Computational Biology
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109
> 
> Location: M1-B861
> Telephone: 206 667-2793
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list