From mtmorgan at fhcrc.org  Sat Mar 26 19:52:28 2011
From: mtmorgan at fhcrc.org (Martin Morgan)
Date: Sat, 26 Mar 2011 11:52:28 -0700
Subject: [Rd] another import puzzle
In-Reply-To: <4D8E2CF4.1030307@gmail.com>
References: <4D8E2CF4.1030307@gmail.com>
Message-ID: <4D8E35EC.20406@fhcrc.org>

On 03/26/2011 11:14 AM, Ben Bolker wrote:
>
>    Dear list,
>
>    I have another (again possibly boneheaded) puzzle about importing,
> again encapsulated in a nearly trivial package.  (The package is posted
> at<http://www.math.mcmaster.ca/bolker/misc/coefsumtest_0.001.tar.gz>.)
>
>    The package consists (only) of the following S3 method definitions:
>
> coeftab<- function(object, ...) UseMethod("coeftab",object)
> coeftab.default<- function(object,...) {
>    print(class(summary(object)))
>    coef(summary(object))
> }
>
>    The NAMESPACE tries to pull in the necessary bits and pieces from lme4
> to extract summaries and coefficients:
>
> export("coeftab","coeftab.default")
> importClassesFrom(lme4,"mer","summary.mer")
> importMethodsFrom(lme4,"coef","summary","show","print")

It 'turns out' that base::summary is an S3 generic. Matrix creates an S4 
generic that is distinct from base::summary (e.g., so that the default 
behavior of summary isn't altered for packages that want to have nothing 
to do with Matrix). Dispatch needs to go through the generic. lme4 has 
methods on Matrix::summary, not on base::summary, so without the 
Matrix::summary generic your object never sees the summary method for 
lme4 objects.

So you need to Import: Matrix and importFrom(Matrix, summary).

Martin Morgan

> exportMethods("coef","summary","show","print")
> exportClasses("mer","summary.mer")
> S3method(coeftab,default)
>
>    The package passes the routine parts of R CMD check.  The following
> test shows that, with lme4 loaded, coef(summary([object of class
> "mer"])) works in the global environment, but not in a function defined
> inside the namespace of the package.
>
>    The output ends with:
>
>> coeftab.default(gm1)
> [1] "summaryDefault" "table"
> Error in object$coefficients : $ operator is invalid for atomic vectors
> Calls: coeftab.default ->  coef ->  coef ->  coef.default
>
>    which indicates that inside the function, summary() is calling
> summary.default instead of seeing the summary method for "mer" objects ...
>
>
>    I have (re-re-re-)read the appropriate R-exts section, without luck,
> and tried various minor variations (e.g. import()ing all of lme4,
> changing the order of the directive, ...).
>
>    Help ... ?
>
>    sincerely
>      Ben Bolker
>
> =====
> test.R
> =====
>
> library(coefsumtest)
> library(lme4)
>
> gm1<- glmer(cbind(incidence, size - incidence) ~ period + (1 | herd),
>               family = binomial, data = cbpp)
>
> coef(summary(gm1)) ## works
>
> f<- function(g) {
>    coef(summary(g))
> }
> f(gm1)  ## works
>
> coeftab.default(gm1) ##
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


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

Location: M1-B861
Telephone: 206 667-2793



[Rd] another import puzzle
Martin Morgan