From simon.urbanek at r-project.org  Sat May  7 21:33:49 2005
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Sat May  7 21:33:51 2005
Subject: [Rd] how to add method to .Primitive function
In-Reply-To: <C698D707214E6F4AB39AB7096C3DE5A59E9334@phost015.EVAFUNDS.intermedia.net>
References: <C698D707214E6F4AB39AB7096C3DE5A59E9334@phost015.EVAFUNDS.intermedia.net>
Message-ID: <4318EAA1-A3B2-43B5-81CF-EF0A9E929F43@r-project.org>

On May 7, 2005, at 2:59 PM, Vadim Ogranovich wrote:


> But then mylist is not a list:
>
>
>> x <- new("mylist", x = list(x = 1, y = 2))
>> x[[1]]
>>
>>
> Error in x[[1]] : subscript out of bounds
>
> This is probably solvable by a sprinkle of setIs or setAs spells, but
> each time I get into the S4 area I feel walking a mine-field.
>

Well, then you can still use S3:
x <- list(x=1:5, y=5:1)
class(x) <- "mylist"
dim.mylist <- function(l) c(length(l[[1]]),length(l))
dim(x)
[1] 5 2
x[[1]]
[1] 1 2 3 4 5
is.list(x)
[1] TRUE

I'm not saying it's a good idea, because you can still break other  
things if you're not careful, but it's possible... If all you want is  
writing convenience functions for lists, that's fine, but I don't  
think you can replace data frames with such objects easily ...

Cheers,
Simon


[Rd] how to add method to .Primitive function
Simon Urbanek