Hi, I am having a problem to understand why as.data.frame method doesn't dispatch properly on my class:> setClass("Foo", "character")[1] "Foo"> as.data.frame(list(foo=new("Foo", .Data="a")))Error in as.data.frame.default(x[[i]], optional = TRUE) : can't coerce Foo into a data.frame I was expecting that this would call as.data.frame.character. Another puzzle. If I explicitly call as.data.frame.character() it would fail but for a different reason:> as.data.frame.character(list(foo=new("Foo", .Data="a")))Error in unique.default(x) : unique() applies only to vectors I was under an impression that an instance of "Foo" would be welcome anywhere a "character" was, but it seems to be more subtle. What am I missing? Thanks, Vadim
On Wed, 17 Mar 2004, Vadim Ogranovich wrote:> I am having a problem to understand why as.data.frame method doesn't > dispatch properly on my class: > > > setClass("Foo", "character") > [1] "Foo" > > as.data.frame(list(foo=new("Foo", .Data="a"))) > Error in as.data.frame.default(x[[i]], optional = TRUE) : > can't coerce Foo into a data.frame > > I was expecting that this would call as.data.frame.character.You have set an S4 class and as.data.frame is an S3 generic.> list(foo=new("Foo", .Data="a"))$foo An object of class "Foo" [1] "a" and what as.data.frame sees is> attributes(list(foo=new("Foo", .Data="a"))$foo)$class [1] "Foo" attr(,"package") [1] ".GlobalEnv" so thinks this is an S3 class it knows nothing about.> Another puzzle. If I explicitly call as.data.frame.character() it would > fail but for a different reason: > > > as.data.frame.character(list(foo=new("Foo", .Data="a"))) > Error in unique.default(x) : unique() applies only to vectors > > I was under an impression that an instance of "Foo" would be welcome > anywhere a "character" was, but it seems to be more subtle. What am I > missing?The difference between S3 and S4 classes. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
I see. Thank you very much! This brings another question. Does R-Core have any plan to promote data.frame to an S4 class? In general, is there any "road-map" (formal or informal) to phasing out S3 classes? Thanks, Vadim> -----Original Message----- > From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] > Sent: Thursday, March 18, 2004 12:24 AM > To: Vadim Ogranovich > Cc: R Help List > Subject: Re: [R] why-s of method dispatching > > > On Wed, 17 Mar 2004, Vadim Ogranovich wrote: > > > I am having a problem to understand why as.data.frame > method doesn't > > dispatch properly on my class: > > > > > setClass("Foo", "character") > > [1] "Foo" > > > as.data.frame(list(foo=new("Foo", .Data="a"))) > > Error in as.data.frame.default(x[[i]], optional = TRUE) : can't > > coerce Foo into a data.frame > > > > I was expecting that this would call as.data.frame.character. > > You have set an S4 class and as.data.frame is an S3 generic. > > > list(foo=new("Foo", .Data="a")) > $foo > An object of class "Foo" > [1] "a" > > and what as.data.frame sees is > > > attributes(list(foo=new("Foo", .Data="a"))$foo) > $class > [1] "Foo" > attr(,"package") > [1] ".GlobalEnv" > > so thinks this is an S3 class it knows nothing about. > > > Another puzzle. If I explicitly call as.data.frame.character() it > > would fail but for a different reason: > > > > > as.data.frame.character(list(foo=new("Foo", .Data="a"))) > > Error in unique.default(x) : unique() applies only to vectors > > > > I was under an impression that an instance of "Foo" would > be welcome > > anywhere a "character" was, but it seems to be more subtle. > What am I > > missing? > > The difference between S3 and S4 classes. > > -- > Brian D. Ripley, ripley at stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > >
I see. Thank you very much! Does R-Core have any plan to promote data.frame to an S4 class? In general, is there any "road-map" (formal or informal) for phasing out S3 classes? Thanks, Vadim> -----Original Message----- > From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] > Sent: Thursday, March 18, 2004 12:24 AM > To: Vadim Ogranovich > Cc: R Help List > Subject: Re: [R] why-s of method dispatching > > > On Wed, 17 Mar 2004, Vadim Ogranovich wrote: > > > I am having a problem to understand why as.data.frame > method doesn't > > dispatch properly on my class: > > > > > setClass("Foo", "character") > > [1] "Foo" > > > as.data.frame(list(foo=new("Foo", .Data="a"))) > > Error in as.data.frame.default(x[[i]], optional = TRUE) : > > can't coerce Foo into a data.frame > > > > I was expecting that this would call as.data.frame.character. > > You have set an S4 class and as.data.frame is an S3 generic. > > > list(foo=new("Foo", .Data="a")) > $foo > An object of class "Foo" > [1] "a" > > and what as.data.frame sees is > > > attributes(list(foo=new("Foo", .Data="a"))$foo) > $class > [1] "Foo" > attr(,"package") > [1] ".GlobalEnv" > > so thinks this is an S3 class it knows nothing about. > > > Another puzzle. If I explicitly call as.data.frame.character() it > > would fail but for a different reason: > > > > > as.data.frame.character(list(foo=new("Foo", .Data="a"))) > > Error in unique.default(x) : unique() applies only to vectors > > > > I was under an impression that an instance of "Foo" would > be welcome > > anywhere a "character" was, but it seems to be more subtle. > What am I > > missing? > > The difference between S3 and S4 classes. > > -- > Brian D. Ripley, ripley at stats.ox.ac.uk > Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ > University of Oxford, Tel: +44 1865 272861 (self) > 1 South Parks Road, +44 1865 272866 (PA) > Oxford OX1 3TG, UK Fax: +44 1865 272595 > >
Vadim Ogranovich wrote:> Does R-Core have any plan to promote data.frame to an S4 class? In > general, is there any "road-map" (formal or informal) for phasing out > S3 classes?FOR GOD'S SAKE DON'T!!! S4 classes and methods are incomprehensible to the human mind, which is what I'm equipped with. If S3 is phased out, R will become effectively unusable as a programming language. cheers, Rolf Turner rolf at math.unb.ca