Re: [R] setAs vs setIs

From: Charilaos Skiadas <cskiadas_at_gmail.com>
Date: Sun, 16 Mar 2008 20:55:48 -0400

On Mar 16, 2008, at 8:12 PM, Christophe Genolini wrote:

> 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

It seems to me your problem here is simply that you did not define a coerce cal in setIs, so it does not know how to turn a C object into a B object, which is what you ask it to do here. It knows how to test if C object is also a B object, because of the test function you provided, and it can do the replacement you ask it in as(c,"B") <-b because of the replace command you provided, but the third part is missing. Perhaps something like this:
setIs("C","B",

	test=function(object){return(object_at_c>0)},
	replace=function(from,values){
		from_at_c <- values_at_b^3
		return(from)
	},
	coerce=function(from) {
		new("B",b=from_at_c^(1/3))
	}

)

> 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

Haris Skiadas
Department of Mathematics and Computer Science Hanover College



R-help_at_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. Received on Mon 17 Mar 2008 - 00:58:27 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 17 Mar 2008 - 09:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive