Hello everybody Is there a good way for a function to tell whether the caller used the defaults? I'm writing a little function that may take a pair of real arguments or a single complex argument (in which case I want the real and imaginary components). "e" <- function(first,second=first) { if (all(first == second) & is.complex(first)) { return(c(Re(first),Im(first))) } else { return (c(Re(first),Re(second))) } } Thus> e(1+5i)[1] 1 5> e(1,5)[1] 1 5 which is what I want, but what is the best way to test whether the second argument is explicitly set by the caller or the default is used? My only idea was to test for first == second but this can't be optimal. Help anyone? -- Robin Hankin, Lecturer, School of Geographical and Environmental Science Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Maybe a better way is: e <- function(first, second) { if(missing(second)) return(c(Re(first), Im(first))) else return(c(Re(first), Re(second))) } I'm not sure if this is what you're going for. -roger _______________________________ UCLA Department of Statistics rpeng at stat.ucla.edu http://www.stat.ucla.edu/~rpeng On Mon, 22 Apr 2002, Robin Hankin wrote:> > Hello everybody > > > Is there a good way for a function to tell whether the caller used the > defaults? > > I'm writing a little function that may take a pair of real arguments > or a single complex argument (in which case I want the real and > imaginary components). > > > "e" <- function(first,second=first) { > if (all(first == second) & is.complex(first)) { > return(c(Re(first),Im(first))) > } else { > return (c(Re(first),Re(second))) > } > } > > > Thus > > > e(1+5i) > [1] 1 5 > > e(1,5) > [1] 1 5 > > > which is what I want, but what is the best way to test whether the > second argument is explicitly set by the caller or the default is > used? My only idea was to test for first == second but this can't be > optimal. Help anyone? > > > -- > > Robin Hankin, Lecturer, > School of Geographical and Environmental Science > Private Bag 92019 Auckland > New Zealand > > r.hankin at auckland.ac.nz > tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042 > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 22 Apr 2002, Robin Hankin wrote:> > Hello everybody > > > Is there a good way for a function to tell whether the caller used the > defaults?Yes, or then again, no. You can use the function missing() to test whether an explicit argument was supplied.> > "e" <- function(first,second=first) { > if (all(first == second) & is.complex(first)) {ie if (missing(second)){> return(c(Re(first),Im(first))) > } else { > return (c(Re(first),Re(second))) > } > } >This will not preserve the missingness f<-function(a,b,c,d=c){ e(c,d) } This will work, but not if you modify d before calling e() f<-function(a,b,c,d){ e(c,d) } That's why we often use NULL as the default: "e" <- function(first,second=NULL) { if (is.null(second)){ return(c(Re(first),Im(first))) } else { return (c(Re(first),Re(second))) } } f<-function(a,b,c,d=NULL){ e(c,d) } -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._