Re: [Rd] S4 class extends "data.frame", getDataPart sees "list"

From: John Chambers <jmc_at_r-project.org>
Date: Tue, 13 Jul 2010 09:11:21 -0700

On 7/13/10 8:43 AM, Daniel Murphy wrote:
> Thank you. For "getDataPart" I was following (my interpretation of)
> advice from the documentation for "Classes": The functions |getDataPart
> <http://127.0.0.1:15455/library/methods/help/getDataPart>| and
> |setDataPart <http://127.0.0.1:15455/library/methods/help/setDataPart>|
> are a cleaner, but essentially equivalent way to deal with the data part.
> I interpreted "cleaner" to mean "preferred." From your reply, John, it
> sounds like I should go back to the object@.Data construct.

No, that's not the distinction. Preferred for what, is the question. In order for a class to extend a basic type, it has to have that type. Then the .Data "slot" is a sort of fiction needed for the metadata. (Because of some "features" of R implementation, it's not quite that simple. Matrices act like a basic type, and some types, such as "environment" require a second kludge.)

If you really want that slot, the advice holds, but only because there is no actual ".Data" slot (i.e., attribute).

But you weren't talking about that at all. In fact (just to contradict my previous mail) you probably wanted to turn your object into a data.frame. If so, best to say so:

 > as(z, "data.frame")
Object of class "data.frame"

   x
1 1
2 2
3 3

and, for that matter:

 > as(z, "list")
[[1]]
[1] 1 2 3

> -Dan
> On Tue, Jul 13, 2010 at 5:57 AM, John Chambers <jmc@r-project.org
> <mailto:jmc_at_r-project.org>> wrote:

>

> On 7/11/10 9:08 PM, Daniel Murphy wrote:
>
> R-Devel:
>
> When I get the data part of an S4 class that
> contains="data.frame", it gives
> me a list, even when the "data.frame" is the S4 version:
>
> d<-data.frame(x=1:3)
> isS4(d)
>
> [1] FALSE # of course
>
> dS4<-new("data.frame",d)
> isS4(dS4)
>
> [1] TRUE # ok
>
> class(dS4)
>
> [1] "data.frame" # good
> attr(,"package")
> [1] "methods"
>
> setClass("A", representation(label="character"),
> contains="data.frame")
>
> [1] "A"
>
> a<-new("A",dS4, label="myFrame")
> getDataPart(a)
>
> [[1]] # oh?
> [1] 1 2 3
>
> class(a@.Data)
>
> [1] "list" # hmm
>
> names(a)
>
> [1] "x" # sure, that makes sense
>
> a
>
> Object of class "A"
> x
> 1 1
> 2 2
> 3 3
> Slot "label":
> [1] "myFrame"
> >
> Was I wrong to have expected the "data part" of 'a' to be a
> "data.frame"?
> >
> Yes. Also, there is a clue in the documentation for getDataPart:
> "rarely suitable to be called directly"
> The data part, aka "data slot", generally does not have a class (S4
> or S3).
>
> You are probably looking for S3Part():
>
> > setClass("myFrame", contains = "data.frame")
> [1] "myFrame"
> > z = new("myFrame", data.frame(x=1:3))
> > z
> Object of class "myFrame"
>
> x
> 1 1
> 2 2
> 3 3
> > S3Part(z)
> Object of class "data.frame"
>
> x
> 1 1
> 2 2
> 3 3
> > S3Part(z, strictS3 = TRUE)
>
> x
> 1 1
> 2 2
> 3 3
> > > >
> Thanks.
>
> Dan Murphy
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel_at_r-project.org <mailto:R-devel_at_r-project.org> mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> > ______________________________________________ R-devel_at_r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Tue 13 Jul 2010 - 16:14:11 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 13 Jul 2010 - 19:10:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive