From jmc at r-project.org  Mon Sep 11 16:05:23 2006
From: jmc at r-project.org (John Chambers)
Date: Mon, 11 Sep 2006 10:05:23 -0400
Subject: [Rd] S4 Method dispatch in recent 2.4.0alpha
In-Reply-To: <40964D91251A18469CB08B6E96FB30BABAD8D0@mailc.lumcnet.prod.intern>
References: <40964D91251A18469CB08B6E96FB30BABAD8D0@mailc.lumcnet.prod.intern>
Message-ID: <45056D23.5040201@r-project.org>

Good example.

The basic problem is in the NAMESPACE file:

importFrom(graphics, plot)

But the version of plot() in the graphics package is not a generic 
function.  Therefore, when your mpplot() calls it there is no method 
dispatch.

You need to import the generic version of plot() from minipkg2 (notice 
that there's a message about creating a new generic for plot() when you 
install minipkg2).

The line in the NAMESPACE file should be:

importFrom(minipkg2, plot)

In your mini-example, there are some additional steps needed, not 
directly related to the problem & possibly not true in the real example.

1.  minipkg2 also needs a NAMESPACE, in which it imports from methods 
and graphics and exports plot and the class mp2.plot (example attached).

2.  Highly recommended though maybe not required here is to use some 
form of saved image, e.g. by including "LazyLoad:yes" in the two 
DESCRIPTION files.

John


Oosting, J. (PATH) wrote:
> I use 2 packages that both implement a S4 plot method, where one package
> depends on the other (the bioconductor package globaltest which depends
> on multtest). When the plot method is used from within the package, it
> seems the default plot method is used, and an error is generated. When
> the method is invoked from the console, the plot is created correctly. I
> have reproduced this with 2 small packages (minipkg and minipkg2)
> implementing just this part.
> I've seen a thread about a similar problem, but that seemed mostly due
> to already installed packages not handling the new S4 stuff.
>
> mpplot() is a function that creates a class instance and (usually)
> invokes the plot immediately. When the dependency on minipkg2 is removed
> from the DESCRIPTION file the first call to mpplot() gives no error and
> shows the plot.
>
>
> Jan Oosting
>
>   
>> library(minipkg)
>>     
> Loading required package: minipkg2
> Creating a new generic function for 'plot' in 'minipkg2'
>   
>> mpplot(1:10)
>>     
> Error in as.vector(x, "double") : cannot coerce to vector
>   
>> plot(mpplot(1:10,plot=FALSE)) # this shows a proper plot
>> showMethods("plot")
>>     
> Function: plot, (package graphics)
> x="ANY"
> x="mp.plot"
> x="mp2.plot"
>   
>> sessionInfo()
>>     
> R version 2.4.0 Under development (unstable) (2006-09-04 r39086) 
> i386-pc-mingw32 
>
> locale:
> LC_COLLATE=Dutch_Netherlands.1252;LC_CTYPE=Dutch_Netherlands.1252;LC_MON
> ETARY=Dutch_Netherlands.1252;LC_NUMERIC=C;LC_TIME=Dutch_Netherlands.1252
>
> attached base packages:
> [1] "methods"   "stats"     "graphics"  "grDevices" "utils"
> "datasets" 
> [7] "base"     
>
> other attached packages:
>  minipkg minipkg2 
>  "1.0.0"  "1.0.0" 
>   
>> ------------------------------------------------------------------------
>>
>> ______________________________________________
>> 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: NAMESPACE
Url: https://stat.ethz.ch/pipermail/r-devel/attachments/20060911/1b86f259/attachment-0003.pl 


[Rd] S4 Method dispatch in recent 2.4.0alpha
John Chambers