Re: [R] Making an S3 object act like a data.frame

2006-03-09 Thread Prof Brian Ripley
On Tue, 7 Mar 2006, hadley wickham wrote: I tend to have to use trial and error myself. Here is another possibility. That's got the subsetting solved, so here's the next challenge lm(x ~ y, z) Error in as.data.frame.default(data) : cannot coerce class myobj into a data.frame

Re: [R] Making an S3 object act like a data.frame

2006-03-09 Thread hadley wickham
I'm guessing this is pretty much impossible to get around, because there is no way to tell eval how to deal with myobj type objects, and lm only dispatches based on the type of the first argument. Did you write an as.data.frame method? From ?model.frame My as.data.frame is :

Re: [R] Making an S3 object act like a data.frame

2006-03-09 Thread Gabor Grothendieck
Can you create a small self contained reproducible example that does not work? The reproducible example I provided earlier on this thread worked fine. One idea is to check what the class is of the output of your .GGobiCall. If it were of class ggobiDataset then it would in turn be calling

Re: [R] Making an S3 object act like a data.frame

2006-03-09 Thread hadley wickham
Can you create a small self contained reproducible example that does not work? The reproducible example I provided earlier on this thread worked fine. I wish I could, and I'm very grateful for the help, but because the data is an external pointer it's not easy to make a self-contained example.

Re: [R] Making an S3 object act like a data.frame

2006-03-09 Thread Gabor Grothendieck
I think the problem is that your ggobiDataset objects are also data.frame objects. They must NOT be. For example, note how the following example fails once we add data.frame to the class vector of x: The reason is that ordinary inheritance is not used by model.frame; rather, it uses

[R] Making an S3 object act like a data.frame

2006-03-07 Thread hadley wickham
[.ggobiDataset - function(x, ..., drop=FALSE) { x - as.data.frame(x) NextMethod([, x) } [[.ggobiDataset - function(x, ..., drop=FALSE) { x - as.data.frame(x) NextMethod([[, x) } $.ggobiDataset - function(x, ..., drop=FALSE) { x - as.data.frame(x)

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread Gabor Grothendieck
If your class is a subclass of data.frame then I think it ought to be a special sort of data frame so all you need to do is the following in which case you get subscripting for free by inheritance: # constructor myobj - function(...) structure(data.frame(...), class = c(myobj,

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread hadley wickham
If your class is a subclass of data.frame then I think it ought to be a special sort of data frame so all you need to do is the following in which case you get subscripting for free by inheritance: My class is actually an external pointer to a data set stored in ggobi, so I don't think this

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread Henrik Bengtsson
Hi. On 3/7/06, hadley wickham [EMAIL PROTECTED] wrote: [.ggobiDataset - function(x, ..., drop=FALSE) { x - as.data.frame(x) NextMethod([, x) } [[.ggobiDataset - function(x, ..., drop=FALSE) { x - as.data.frame(x) NextMethod([[, x) } $.ggobiDataset -

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread hadley wickham
x$total_bill Error in $.default(x, total_bill) : invalid subscript type where/how is $.default() defined? I don't have one; getAnywhere($.default) no object named '$.default' was found Neither do I. I guess there is some internal magic going on for $. Hadley

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread Gabor Grothendieck
Just to get something reproducible lets assume the objects of class myobj each consists of a one-element list that contains a data frame. Then try this: # constructor myobj - function(...) structure(list(value = data.frame(...)), class = myobj) $.myobj - function(obj, x) .subset2(obj,

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread hadley wickham
Just to get something reproducible lets assume the objects of class myobj each consists of a one-element list that contains a data frame. Then try this: Thanks for that - it makes sense. Every time I try to use inheritance in R, I always seem to end up using a different method. Hadley

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread Gabor Grothendieck
On 3/7/06, hadley wickham [EMAIL PROTECTED] wrote: Just to get something reproducible lets assume the objects of class myobj each consists of a one-element list that contains a data frame. Then try this: Thanks for that - it makes sense. Every time I try to use inheritance in R, I

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread hadley wickham
I tend to have to use trial and error myself. Here is another possibility. That's got the subsetting solved, so here's the next challenge lm(x ~ y, z) Error in as.data.frame.default(data) : cannot coerce class myobj into a data.frame as.data.frame.myobj - function(x) x[[1]] lm(x ~ y, z)

Re: [R] Making an S3 object act like a data.frame

2006-03-07 Thread Gabor Grothendieck
The problem is that x[[1]] in the definition of as.data.frame.myobj invokes [[.myobj whereas we want to extract the first element of the list in the internal representation of x -- which is not the same. Try this instead: as.data.frame.myobj - function(x) .subset2(x, 1) lm(y ~ x, z) Call: