[R] setAs vs setIs

Christophe Genolini cgenolin at u-paris10.fr
Mon Mar 17 01:12:51 CET 2008


Hi the list

I am fighting with the twins setAs and setIs...

Here are some questions and comments (comments to myself but that migth 
be wrong, it is why I am posting them)
1. Very surprising : using setIs define 'is', 'as<-' but not 'as' ???
2. Using setAs define 'as', 'as<-' but not 'is'...
What surprise me is that as<- can be define by both. I would have thing 
that setis is for 'is', setAs is for 'as' and 'as<-'...
Since it is not the case, is there a possibility to set with only one 
function 'as', 'is' and 'as<-'

Last point, I get a warning using setAs. I did not manage to find the 
name of the variable it want me to use...

### Data
setClass("B",representation(b="numeric"))
setClass("C",representation(c="numeric"))
setClass("D",representation(d="numeric"))
b <- new("B",b=3)
c <- new("C",c=4)
d <- new("D",d=5)

### using setIs
setIs("C","B",
    test=function(object){return(object at c>0)},
    replace=function(from,values){
        from at c <- values at b^3
        return(from)
    }
)
is(c,"B")             #Ok
as(c,"B")             #not ok
as(c,"B") <- b        #ok (!)

### using setAs
setAs("D","B",
    function(from,to){to<-new("B",b=from at d);return(to)},
    replace=function(from,values){
        from at d<-values at b^2;
        return(from)
    }
)
is(d,"B")             # not ok
as(d,"B")             # ok
as(d,"B")<-b          # ok


Thanks

Christophe



More information about the R-help mailing list