[Rd] S4: what to put in initialize, validity and constructor?
Hi the list, I have some trouble using validity, intialize and the constructor. More precisely, what should go where? Here is a toy exemple (seams long, but the code is very simple): I want to define an object with two slots a and b with the properties that b will be either empty or the scare of a. Example of valid object : a= b= a=2 b= a=3 b=9 So I define my object and the validity function : setClass( "A", representation(a="numeric",b="numeric"), validity=function(object){ cat("Validity\n") if(length([EMAIL PROTECTED])!=0){ if(length([EMAIL PROTECTED])==0){stop("Can not have empty a and non emty b")}else{} if([EMAIL PROTECTED]@b){stop("b is not the scare of a")}else{} }else{} return(TRUE) } ) It works: new("A") new("A",a=2,b=4) try(new("A",b=4)) new("A",a=2) try(new("A",a=2,b=3)) Then I define the initialize function. When b is givent but not a, the initialise function set a to sqrt(b). setMethod( "initialize", "A", function(.Object,a,b){ if(missing(a)&!missing(b)){ [EMAIL PROTECTED] <- b [EMAIL PROTECTED] <- sqrt(b) }else{} if(!missing(a)&missing(b)){ [EMAIL PROTECTED] <- a }else{} if(!missing(a)&!missing(b)){ [EMAIL PROTECTED] <- a [EMAIL PROTECTED] <- b }else{} validObject(.Object) return(.Object) } ) It is fine: new("A") new("A",a=2,b=4) new("A",b=9) new("A",a=2) try(new("A",a=2,b=3)) Then I want to set the constructor A <- function(a,b){ return(new("A",a,b)) } But this does not work: A() A(a=2,b=4) A(b=9) A(a=2) The following does not work either: A <- function(a=numeric(),b=numeric()){ return(new("A",a,b)) } A() A(a=2,b=4) A(b=9) A(a=2) So is there a way to define the constructor A without dealing again with all the missing&missing staff like in initialize? Christophe __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4: what to put in initialize, validity and constructor?
setClass("A", representation(a="numeric",b="numeric"), validity = function(object) { if (length([EMAIL PROTECTED])!=length([EMAIL PROTECTED])) return("different lengths of a and b") if (length([EMAIL PROTECTED])==0) return("object length is zero") TRUE } ) Do not change initialize! Define constructors: setGeneric("A", function(a,b,...) standardGeneric("A")) setMethod("A", signature(a="missing",b="missing"), function(a,b,...) A(as.numeric(1:10),...) ## calls the one below ) setMethod("A", signature(a="A",b="missing"), function(a,b,...) a ) setMethod("A", signature(a="ANY",b="ANY"), function(a,b,...) new("A",a=as.numeric(a),b=as.numeric(b),...) ) setMethod("A", signature(a="ANY",b="missing"), function(a,b,...) A(a,a,...) ## Calls the one above ) etc. In words: 1) validity should return a character in case of errors 2) default initializer usually does the job 3) define constructors as methods to allow different signatures and conversions from other classes 4) If you derive your class from numeric, rather than add slots, the performance will be much better and you will get default behaviour of numeric, i.e. setClass("A", representatiom("numeric", b="numeric") etc Dr Oleg Sklyar Technology Group Man Investments Ltd +44 (0)20 7144 3803 [EMAIL PROTECTED] > -Original Message- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of > [EMAIL PROTECTED] > Sent: 02 May 2008 15:41 > To: r-devel@r-project.org > Subject: [Rd] S4: what to put in initialize, validity and constructor? > > Hi the list, > > I have some trouble using validity, intialize and the > constructor. More precisely, what should go where? > Here is a toy exemple (seams long, but the code is very simple): > > I want to define an object with two slots a and b with the > properties that b will be either empty or the scare of a. > Example of valid object : > a= b= > a=2 b= > a=3 b=9 > > So I define my object and the validity function : > > setClass( > "A", > representation(a="numeric",b="numeric"), > validity=function(object){ > cat("Validity\n") > if(length([EMAIL PROTECTED])!=0){ > if(length([EMAIL PROTECTED])==0){stop("Can not have empty > a and non emty b")}else{} > if([EMAIL PROTECTED]@b){stop("b is not the scare > of a")}else{} > }else{} > return(TRUE) > } > ) > > It works: > > new("A") > new("A",a=2,b=4) > try(new("A",b=4)) > new("A",a=2) > try(new("A",a=2,b=3)) > > > Then I define the initialize function. When b is givent but > not a, the initialise function set a to sqrt(b). > > setMethod( > "initialize", > "A", > function(.Object,a,b){ > if(missing(a)&!missing(b)){ > [EMAIL PROTECTED] <- b > [EMAIL PROTECTED] <- sqrt(b) > }else{} > if(!missing(a)&missing(b)){ > [EMAIL PROTECTED] <- a > }else{} > if(!missing(a)&!missing(b)){ > [EMAIL PROTECTED] <- a > [EMAIL PROTECTED] <- b > }else{} > validObject(.Object) > return(.Object) > } > ) > > It is fine: > > new("A") > new("A",a=2,b=4) > new("A",b=9) > new("A",a=2) > try(new("A",a=2,b=3)) > > > Then I want to set the constructor > > A <- function(a,b){ > return(new("A",a,b)) > } > > But this does not work: > A() > A(a=2,b=4) > A(b=9) > A(a=2) > > > The following does not work either: > > A <- function(a=numeric(),b=numeric()){ > return(new("A",a,b)) > } > > A() > A(a=2,b=4) > A(b=9) > A(a=2) > > So is there a way to define the constructor A without dealing > again with all the missing&missing staff like in initialize? > > Christophe > > __ > R-devel@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel > ** The contents of this email are for the named addressee(s...{{dropped:22}} __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] S4: what to put in initialize, validity and constructor?
Do not change initialize! As I sat, this is a toy example. In my real example, initialize does a lot of things like calculation of quality indice (b is not the scare of a, but B1, B2 and B3 are the the within matrix of A after imputation with 3 differents methods), giving names to some matrix column and so on. So I seams costfull to not use an initialize. Define constructors: setGeneric("A", function(a,b,...) standardGeneric("A")) setMethod("A", signature(a="missing",b="missing"), function(a,b,...) A(as.numeric(1:10),...) ## calls the one below ) setMethod("A", signature(a="A",b="missing"), function(a,b,...) a ) setMethod("A", signature(a="ANY",b="ANY"), function(a,b,...) new("A",a=as.numeric(a),b=as.numeric(b),...) ) setMethod("A", signature(a="ANY",b="missing"), function(a,b,...) A(a,a,...) ## Calls the one above ) etc. In words: 1) validity should return a character in case of errors 2) default initializer usually does the job 3) define constructors as methods to allow different signatures and conversions from other classes 4) If you derive your class from numeric, rather than add slots, the performance will be much better and you will get default behaviour of numeric, i.e. setClass("A", representatiom("numeric", b="numeric") etc Dr Oleg Sklyar Technology Group Man Investments Ltd +44 (0)20 7144 3803 [EMAIL PROTECTED] -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of [EMAIL PROTECTED] Sent: 02 May 2008 15:41 To: r-devel@r-project.org Subject: [Rd] S4: what to put in initialize, validity and constructor? Hi the list, I have some trouble using validity, intialize and the constructor. More precisely, what should go where? Here is a toy exemple (seams long, but the code is very simple): I want to define an object with two slots a and b with the properties that b will be either empty or the scare of a. Example of valid object : a= b= a=2 b= a=3 b=9 So I define my object and the validity function : setClass( "A", representation(a="numeric",b="numeric"), validity=function(object){ cat("Validity\n") if(length([EMAIL PROTECTED])!=0){ if(length([EMAIL PROTECTED])==0){stop("Can not have empty a and non emty b")}else{} if([EMAIL PROTECTED]@b){stop("b is not the scare of a")}else{} }else{} return(TRUE) } ) It works: new("A") new("A",a=2,b=4) try(new("A",b=4)) new("A",a=2) try(new("A",a=2,b=3)) Then I define the initialize function. When b is givent but not a, the initialise function set a to sqrt(b). setMethod( "initialize", "A", function(.Object,a,b){ if(missing(a)&!missing(b)){ [EMAIL PROTECTED] <- b [EMAIL PROTECTED] <- sqrt(b) }else{} if(!missing(a)&missing(b)){ [EMAIL PROTECTED] <- a }else{} if(!missing(a)&!missing(b)){ [EMAIL PROTECTED] <- a [EMAIL PROTECTED] <- b }else{} validObject(.Object) return(.Object) } ) It is fine: new("A") new("A",a=2,b=4) new("A",b=9) new("A",a=2) try(new("A",a=2,b=3)) Then I want to set the constructor A <- function(a,b){ return(new("A",a,b)) } But this does not work: A() A(a=2,b=4) A(b=9) A(a=2) The following does not work either: A <- function(a=numeric(),b=numeric()){ return(new("A",a,b)) } A() A(a=2,b=4) A(b=9) A(a=2) So is there a way to define the constructor A without dealing again with all the missing&missing staff like in initialize? Christophe __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel ** The contents of this email are for the named addressee(s) only. It contains information which may be confidential and privileged. If you are not the intended recipient, please notify the sender immediately, destroy this email and any attachments and do not otherwise disclose or use them. Email transmission is not a secure method of communication and Man Investments cannot accept responsibility for the completeness or accuracy of this email or any attachments. Whilst Man Investments makes every effort to keep its network free from viruses, it does not accept responsibility for any computer virus which might be transferred by way of this email or any attachments. This email does not constitute a request, offer, recommendation or solicitation of any kind to buy, subscribe, sell or redeem any investment instruments or to perform other such transactions of any kind. Man Investments reserves the right to monitor, record and retain all electronic communications through its network to ensure the integrity of its systems, for record keeping and regulatory purposes. Visit us at: www.maninvestments.com ** __ R-
[Rd] Out of bounds negative index
Hi, From the R Language Definition, Section 3.4.1: "If i is positive and exceeds length(x) then the corresponding selection is NA. A negative out of bounds value for i causes an error." (This is also mentioned in S Programming, footnote of page 24.) Can someone please provide an example triggering the error? Looking in src/main/subscript.c I could not find exception handling for |i| > length(x), unless the negative subscript is mixed with NAs. In other cases, out of bounds negative indexes just seem silently ignored. Did I missed or misinterpreted something, here? Thanks in advance --- Vincent Goulet, Associate Professor École d'actuariat Université Laval, Québec [EMAIL PROTECTED] http://vgoulet.act.ulaval.ca __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Out of bounds negative index
On Fri, May 2, 2008 at 2:23 PM, Vincent Goulet <[EMAIL PROTECTED]> wrote: > Hi, > > From the R Language Definition, Section 3.4.1: > > "If i is positive and exceeds length(x) then the corresponding selection is > NA. A negative out of bounds value for i causes an error." > > (This is also mentioned in S Programming, footnote of page 24.) > > Can someone please provide an example triggering the error? Looking in > src/main/subscript.c I could not find exception handling for |i| > > length(x), unless the negative subscript is mixed with NAs. In other cases, > out of bounds negative indexes just seem silently ignored. > > Did I missed or misinterpreted something, here? Well, there's this: > a <- 1:10 > a[NA] [1] NA NA NA NA NA NA NA NA NA NA > a[-NA] [1] NA Hadley -- http://had.co.nz/ __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Out of bounds negative index
On 5/2/2008 3:23 PM, Vincent Goulet wrote: Hi, From the R Language Definition, Section 3.4.1: "If i is positive and exceeds length(x) then the corresponding selection is NA. A negative out of bounds value for i causes an error." (This is also mentioned in S Programming, footnote of page 24.) Can someone please provide an example triggering the error? Looking in src/main/subscript.c I could not find exception handling for |i| > length(x), unless the negative subscript is mixed with NAs. In other cases, out of bounds negative indexes just seem silently ignored. Did I missed or misinterpreted something, here? Looks to me like a documentation error. I would expect from that description that executing > x <- 1:5 > x[7] [1] NA > x[-7] [1] 1 2 3 4 5 would have given an error on x[-7], but clearly it didn't. This behaviour appears to have started with 2.6.0; 2.5.1 gives the error. I don't see a NEWS entry about it...but revision r42123 says Changed the behaviour of out-of-bounds negative subscripts to match that of S. Such values are now ignored rather than tripping an error. so apparently it was intentional. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Out of bounds negative index
On 5/2/2008 3:29 PM, hadley wickham wrote: On Fri, May 2, 2008 at 2:23 PM, Vincent Goulet <[EMAIL PROTECTED]> wrote: Hi, From the R Language Definition, Section 3.4.1: "If i is positive and exceeds length(x) then the corresponding selection is NA. A negative out of bounds value for i causes an error." (This is also mentioned in S Programming, footnote of page 24.) Can someone please provide an example triggering the error? Looking in src/main/subscript.c I could not find exception handling for |i| > length(x), unless the negative subscript is mixed with NAs. In other cases, out of bounds negative indexes just seem silently ignored. Did I missed or misinterpreted something, here? Well, there's this: a <- 1:10 a[NA] [1] NA NA NA NA NA NA NA NA NA NA a[-NA] [1] NA But NA and -NA are different: the first is logical, the latter is an integer. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Out of bounds negative index
Le ven. 02 mai à 15:46, Duncan Murdoch a écrit : On 5/2/2008 3:23 PM, Vincent Goulet wrote: Hi, From the R Language Definition, Section 3.4.1: "If i is positive and exceeds length(x) then the corresponding selection is NA. A negative out of bounds value for i causes an error." (This is also mentioned in S Programming, footnote of page 24.) Can someone please provide an example triggering the error? Looking in src/main/subscript.c I could not find exception handling for | i| > length(x), unless the negative subscript is mixed with NAs. In other cases, out of bounds negative indexes just seem silently ignored. Did I missed or misinterpreted something, here? Looks to me like a documentation error. I would expect from that description that executing > x <- 1:5 > x[7] [1] NA > x[-7] [1] 1 2 3 4 5 would have given an error on x[-7], but clearly it didn't. This behaviour appears to have started with 2.6.0; 2.5.1 gives the error. I don't see a NEWS entry about it...but revision r42123 says Changed the behaviour of out-of-bounds negative subscripts to match that of S. Such values are now ignored rather than tripping an error. Ah ha, there it is. The svn log is one place I didn't look. I had screened NEWS, ONEWS and OONEWS, though... Thanks, Duncan. so apparently it was intentional. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel