[R] how can a function tell if defaults are used?

Thomas Lumley tlumley at u.washington.edu
Mon Apr 22 17:10:16 CEST 2002


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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list