[Rd] another import puzzle
Ben Bolker
bbolker at gmail.com
Sat Mar 26 20:19:37 CET 2011
On 11-03-26 02:52 PM, Martin Morgan wrote:
> 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
>
Thank you! The sun truly never sets on R-devel ...
Did you know this from [painful] experience, or is there some sensible
diagnostic procedure I could have followed to track this down?
(I will second the recent comment
<http://r.789695.n4.nabble.com/Namespace-dependency-not-required-td3404575.html>
that "Namespace dependency not required: lme4a" is not very clear. In
the spirit of improvement I attach a proposed patch for
src/library/tools/R/QC.R ... I think "dependence" works better than
"dependency" in English, too: e.g.
<http://newsgroups.derkeiler.com/Archive/Alt/alt.usage.english/2007-07/msg03530.html>.)
Ben Bolker
>> 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
>
>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: QC_R.txt
URL: <https://stat.ethz.ch/pipermail/r-devel/attachments/20110326/0724c3b5/attachment.txt>
More information about the R-devel
mailing list