[R] s3 methods on S4 objects

Martin Morgan mtmorgan at fhcrc.org
Thu Jan 13 15:46:41 CET 2011


On 01/12/2011 10:54 PM, steven mosher wrote:
> I have J Chambers wonderful text ( Software for data Analysis) and I've been
> trying
> my hand at some very routine S4 OOP development.
> 
> One of the things I was trying to do was to create some very basic S4
> classes. The first
> was simply a class that had a data.frame as part of its representation.
> 
> setClass("df",representation(dirframe="data.frame"))
> 
> The object basically contains a data.frame that represents a file directory
> listing
> with a column named filename, size, time, etc.
> 
> And then I have methods for doing various things with this object.
> 
> I then tried to tackle the problem of coercing this S4 object to a
> data.frame. Again just a learning exercise.
> 
> The goal would be able to make a call like this
> 
> testFrame <- as.data.frame(x)
> 
> where x, was an object of class  "df"
> 
> If I try to define "as.data.frame" as a S4 method, then I can make it work,
> but I then destroy the S3 functionality
> of as.data.frame, so that if I were to try to coerce a matrix to a
> data.frame it would work.

Hi Steven --

This works for  me

setClass("A", representation=representation(df="data.frame"))

setMethod("as.data.frame", "A",
    function(x, row.names=NULL, optional=FALSE, ...)
{
    ## implementation, e.g.,
    callGeneric(x at df, row.names=row.names, optional=optional, ...)
})

> as.data.frame(new("A"))
Object of class "data.frame"
data frame with 0 columns and 0 rows
> as.data.frame(matrix(0, 3, 5))
  V1 V2 V3 V4 V5
1  0  0  0  0  0
2  0  0  0  0  0
3  0  0  0  0  0


Maybe you call setGeneric (no need to, setMethod will promote
as.data.frame automatically) in a way that does not specify the default
(arg useAsDefault) correctly?

Martin


> 
> 
> So, I guess my question is what do I do, write an s3 method for
> as.data.frame that takes  a "df" object as a paramter?
> The book wasn't exactly clear ( or I'm not that bright), or is there a way
> to make the S4 method I wrote "as.data.frame"
> call the S3 method if needed?
> 
> 	[[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