[R] importing S4 methods using a namespace

Martin Morgan mtmorgan at fhcrc.org
Wed Feb 24 21:31:01 CET 2010


Hi Aaron --

On 02/23/2010 01:30 PM, Aaron Rendahl wrote:
> I want to call summary on a mer object (from lme4) within my package
> but I can't seem to get the namespace to import the necessary method.
> 
> I've simplified my package to this one function:
> ---------------------------
> ss <- function(m) {
>   summary(m)
> }
> ---------------------------
> 
> And my namespace file looks like this, where I've attempted to follow
> the instructions in "Writing R Extensions"
> http://cran.r-project.org/doc/manuals/R-exts.html#Name-spaces-with-S4-classes-and-methods
> ---------------------------
> import(lme4)
> importMethodsFrom(lme4, "summary")
> export("ss")
> ---------------------------
> 
> But when I call my new function, I get the summary.default method
> instead of the mer method.
> 
>> m <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
>> ss(m)
> Length  Class   Mode
>      1    mer     S4

I think what is going on is that lme4 defines methods on
Matrix::summary. When you importMethodsFrom(lme4, summary) you get the
method but not a generic for them to be hung on -- probably a warning /
error should be generated, or Matrix::summary imported as well.

If you add Imports: Matrix, lme4 to DESCRIPTION, and

importFrom(Matrix, summary)
importMethodsFrom(lme4, summary)

you'll have both a generic and a method to hang on it. I think this is
really a work-around rather than an explanation; I wouldn't really be
able to tell you how I think methods, name spaces, and generics _should_
play together, let alone how they do.

Martin

> 
> 
> Thanks,
> 
> 


-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the R-help mailing list