From michal2992 at gmail.com  Tue May 19 14:57:43 2009
From: michal2992 at gmail.com (=?ISO-8859-2?Q?Micha=B3_Bojanowski?=)
Date: Tue, 19 May 2009 14:57:43 +0200
Subject: [Rd] S4 method dispatch and namespaces: why is default method
	selected
In-Reply-To: <6phr5ylagjr.fsf@gopher4.fhcrc.org>
References: <e1fa237d0905180631h5a6144ao5cf5c65b22bb9fce@mail.gmail.com> 
	<6phr5ylagjr.fsf@gopher4.fhcrc.org>
Message-ID: <e1fa237d0905190557o32c84b60u6ee83058220a4c71@mail.gmail.com>

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.


Micha?



[Rd] S4 method dispatch and namespaces: why is default method selected
=?ISO-8859-2?Q?Micha=B3_Bojanowski?=