[R] Help need to define method of an s4 class

Martin Morgan mtmorgan at fhcrc.org
Tue Feb 1 18:43:25 CET 2011


On 02/01/2011 07:31 AM, Megh Dal wrote:
> I need some help in defining a "print" method for my new S4 class
> definition. So fer I have worked like this:
> 
> setClass("MyClass", sealed=F, representation(slot1 = "list",    #create a
> new class
>             slot2        = "vector",
>             slot3      = "vector",
>             slot4   = "vector"))
> setMethod("print", "MyClass", function(x) {   #set up print method for that
>          cat("My Method definition.\n")
>         })
> newObject <- new("MyClass", slot1=list(diag(3), diag(3)), slot2="some
> character", slot3="some character", slot4="some character")
> print(newObject)
> newObject
> 
> Here what is happening is that, if user types "newObject" then the entire
> structure of this object is displayed nakedly. I want to display the same
> thing which is displayed if user type "print(newObject)".
> 
> If someone guides me how to do that, it would be great.

write a 'show' method for S4 classes, rather than 'print'.

Martin

> 
> Thanks in advance
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


-- 
Computational Biology
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109

Location: M1-B861
Telephone: 206 667-2793



More information about the R-help mailing list