Define S4 methods for 'plot'

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Define S4 methods for 'plot'

cgenolin
Hi the list
I am creating a package and I have a problem to define a S4 method for plot.
I define a class 'A' and a class 'B'. I want to define a function plot for signature c(A,missing) and another method plot for signature c(A,B). My code is the following :

In /package/R/ directory:
--- main.R ---
setGeneric("plot",function(x,y,...){standardGeneric("plot")})
Aplot <- function(x,paramTraj=3){. . . .}
setMethod("plot",signature=c(x="A",y="missing"),Aplot)

ABplot <- function(x,y,paramTraj=5){. . . .}
setMethod("plot",signature=c(x="A",y="B"),ABplot)
---------------

In the root directory /package/
--- NAMESPACE ---
exportMethods("plot",. . . .)
exportClasses("A","B")
------
When I run the code (source("main.r")), every thinks works fine, either plot on object of class 'A', on 'A,B' or on numeric plot(3).

The R CMD check bug on an example using plot(3).
The R CMD build works just fine. But if I try to install the package from the builded file, I get the message:
The following object(s) are masked from 'package:graphics':
    plot.

And then, I cannot use the classical function plot: plot on object 'A' works, but plot(1) does not.

I try, reading some recent post on r-devel to remove the line 'setGeneric' but it does not works (which does not surprise me since getGeneric("plot") gives a NULL results).
 
Any idea of what goes wrong?

Christophe
Reply | Threaded
Open this post in threaded view
|

Re: Define S4 methods for 'plot'

Kevin Coombes
You probably need the directive
     importFrom(graphics, "plot")
in your NAMESPACE file.  This lets the system know that you are using
the same "plot" function that it already knows about. And your code
should be careful not to trash a previous conversion of plot to an S4
generic function, usually by something like

    if (!isGeneric("plot"))
      setGeneric("plot", function(x, y, ...) standardGeneric("plot"))

Best,
     Kevin

On 11/9/2011 12:08 PM, cgenolin wrote:

> Hi the list
> I am creating a package and I have a problem to define a S4 method for plot.
> I define a class 'A' and a class 'B'. I want to define a function plot for
> signature c(A,missing) and another method plot for signature c(A,B). My code
> is the following :
>
> In /package/R/ directory:
> --- main.R ---
> setGeneric("plot",function(x,y,...){standardGeneric("plot")})
> Aplot<- function(x,paramTraj=3){. . . .}
> setMethod("plot",signature=c(x="A",y="missing"),Aplot)
>
> ABplot<- function(x,y,paramTraj=5){. . . .}
> setMethod("plot",signature=c(x="A",y="B"),ABplot)
> ---------------
>
> In the root directory /package/
> --- NAMESPACE ---
> exportMethods("plot",. . . .)
> exportClasses("A","B")
> ------
> When I run the code (source("main.r")), every thinks works fine, either plot
> on object of class 'A', on 'A,B' or on numeric plot(3).
>
> The R CMD check bug on an example using plot(3).
> The R CMD build works just fine. But if I try to install the package from
> the builded file, I get the message:
> The following object(s) are masked from 'package:graphics':
>      plot.
>
> And then, I cannot use the classical function plot: plot on object 'A'
> works, but plot(1) does not.
>
> I try, reading some recent post on r-devel to remove the line 'setGeneric'
> but it does not works (which does not surprise me since getGeneric("plot")
> gives a NULL results).
>
> Any idea of what goes wrong?
>
> Christophe
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Define-S4-methods-for-plot-tp4020750p4020750.html
> Sent from the R devel mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Define S4 methods for 'plot'

cgenolin
I works, thank you very much.