[R] New methods for generic functions show and print : some visible with ls(), some not

Uwe Ligges ligges at statistik.tu-dortmund.de
Fri Feb 26 19:46:55 CET 2010



On 26.02.2010 19:04, Joris Meys wrote:
> Dear all,
>
> I'm trying to understand the S4 way of object-oriented programming, but I
> still can't grasp completely what R is doing. I have a class definition for
> a class called PM10Meteo, and I set a initializer function. next, I include
> a show method and a print method as shown below.
>
> setClass( Class="PM10Meteo",...) # end setClass
>
> setMethod ("initialize",signature="PM10Meteo",...) # end setMethod
>
> ########
> # Show Method
> #
> setMethod("show","PM10Meteo",
>    function(object){
> ...
>    } # end function
> ) # end show method
>
> #######
> # Print Method
> #
>
> setMethod("print","PM10Meteo",
>    function(x,n=400,station=F,values=T,meteo=T,...){
> ...
>    }
> ) # end print method


print is not a S4 generic. show methods are mapped to print for 
convenience, though.

Uwe Ligges



> Now when I run the complete class definition file, neither the method
> "initialize" nor the method "show" occur in the list given by ls(). On the
> other hand, the method "print" does! So when I use rm(list=ls()) I still
> have the "initialize" and "show" method, but the "print" method is gone. Am
> I forgetting something somewhere? It's rather inconvenient to have to run
> the definition files every time I want to clear the memory.
>
> Cheers
> Joris



More information about the R-help mailing list