[Rd] S4 method dispatch and namespaces: why is default method selected

Michał Bojanowski michal2992 at gmail.com
Tue May 19 14:57:43 CEST 2009

Hi Martin,

>> However, when I put all these in a package with Depends field in
>> DESCRIPTION having: methods, pixmap, and with the following namespace:
>> importFrom("graphics", "plot")
>> exportClasses("myclass")
>> exportMethods("plot")
>> things stop working (this is from package test):
>>> f <- system.file(file.path("pictures", "logo.ppm"), package="pixmap")
>>> o <- new("myclass", a=read.pnm(f), title="tytul")
>>> plot(o)
>> Error in as.double(y) :
>>   cannot coerce type 'S4' to vector of type 'double'
>> Calls: plot ... plot -> .local -> plot -> plot.default -> xy.coords
>> Execution halted
>> So apparently calling 'plot' dispatches the default method instead of using the
>> one from package 'pixmap'. Why?
> Not speaking authoritatively, but plot,myclass,missing-method is
> defined in your name space, and so has access only to symbols defined
> or imported into the name space. The plot,pixmap,ANY-method is not in
> your name space (even though it's on the search path), and so is not
> found. Clever of the methods package to keep track of this.

Thanks a lot, I was not aware of that. Given that 'pixmap' package does not
have a namespace I was somehow convinced that all the classes and methods
provided by this package will be visible for other packages. In fact, I was
also convinced that the '::' operator applies only to packages with namespaces.
Which is not the case and the help page for :: is clear about that.

> the only solution is
>  pixmap::plot(x=x at img, ...)

And this works, thanks again.


More information about the R-devel mailing list