Okay, I found something that is working, but it looks and feels pretty 
awkward as the method def and method lookup takes place in one function ;-)

setRefClass("A", fields=list(X="numeric"))
setRefClass("B", contains="A", fields=list(Y="character"))

mySetAs <- function(
     from,
     to
){
     if(!existsMethod(f="coerce", signature=c(from=class(from), to=to))){
         setAs(from=class(from), to=to,
             def=function(from, to){
                 out <- getRefClass(to)$new(X=from)
                 return(out)
             }
         )
     }
     mthd <- selectMethod("coerce", signature=c(from=class(from), 
to=to), useInherited= c(from=TRUE, to=TRUE))
     out <- mthd(from=from, to=to)
     return(out)
}

a <- mySetAs(from=1:5, to="A")
a$X
b <- mySetAs(from=1:5, to="B")
b$X

I'm sure there are much better ways. I'd appreciate any comments whatsoever.

Best regards,
Janko

On 06.06.2011 17:46, Janko Thyson wrote:
> Somehow I don't see my own postings in the list, so sorry for replying 
> to my own message and not the one that went out to the list.
>
> I got a little further and I think I found exactly the thing that is 
> bothering me: how to get "extended" method dispatch going in 'setAs()':
>
> setRefClass("A", fields=list(X="numeric"))
> setRefClass("B", contains="A", fields=list(Y="character"))
>
> setAs(from="numeric", to="A",
>     def=function(from,to){
>         out <- getRefClass(to)$new(X=from)
>         return(out)
>     }
> )
> a <- as(1:5, "A")
> a$X
>
> b <- as(1:5, "B")
>
> My problem is the last statement (b <- as(1:5, "B") which fails. I 
> want to get around having to write new 'setAs' methods for all classes 
> extending class 'A'. If 'B' inherits from 'A', shouldn't it then be 
> possible to tell 'setAs' to look for the next suitable method, i.e. 
> the method defined for 'A'? I tried 'NextMethod()' inside the body of 
> 'setAs' but that didn't work out.
>
> Thanks a lot,
> Janko
>
> On 06.06.2011 17:15, Janko Thyson wrote:
>> Dear list,
>>
>> I wonder how to write methods for the function 'as' in the sense that 
>> I can call 'as(object, Class, strict=TRUE, ext)' and let method 
>> dispatch figure out the correct method.
>>
>> AFAIU, there is a difference between, e.g. 'as.data.frame' and the 
>> methods of 'as()' as stated above since the former depends on arg 'x' 
>> instead of 'object', 'Class' etc.?
>>
>> > methods("as")
>> > as.data.frame
>>
>> I have to admit that I'm not really familiar with the S3 style of 
>> defining methods as I have been coding in S4 a lot, but my first 
>> attempt was to write something like this:
>>
>> as.myClass <- function(x, ...){
>>     if(is(x, "data.frame"){
>>         x <- as.list(x)
>>     }
>>     if(is(x, "character"){
>>         x <- as.list(x)
>>     }
>>     ...
>>     out <- getRefClass("myClass")$new(X=x)
>>     return(out)
>> }
>>
>> But that way I'd have to explicitly call 'as.myClass(x)' whereas I'd 
>> simply like to type 'as(x, "myClass")'.
>> Also, how is it possible to have method dispatch recognize two  
>> signature arguments in S3? I.e., how can I define something like 
>> 'as.data.frame.character' in order to have explicit "sub" methods for 
>> all the data types of 'x' so I wouldn't have to process them all in 
>> the definition of 'as.myClass' as I did above?
>>
>> Thanks for your help,
>> Janko

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to