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

Gabor Grothendieck ggrothendieck at gmail.com
Tue Mar 7 23:44:05 CET 2006

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, 1)[[x]]
"[[.myobj" <- function(obj, ...) .subset2(obj, 1)[[...]]
"[.myobj" <- function(obj, ...) .subset2(obj, 1)[...]

# test
x <- myobj(x = 1:3, y = 4:6)

On 3/7/06, hadley wickham <h.wickham at gmail.com> wrote:
> > 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 will work.  as.data.frame.ggobiDataset
> retrieves the whole dataset out of ggobi and in to R.
> Hadley

More information about the R-help mailing list