Re: [R] Can you recover default argument values of a function?

From: Talbot Katz <topkatz_at_msn.com>
Date: Wed, 26 Dec 2007 18:09:04 -0500

Thank you, that's just what I wanted. By the way, I found an interesting "gotcha" that can occur with expression arguments:

> x = 7
> z = 2
> mxy <- function( x = 4, y = x + z ) { return(x*y) }
> eval( formals( mxy )[[1]] )

[1] 4
> eval( formals( mxy )[[2]] )

[1] 9
> mxy()

[1] 24
> mxy( eval( formals( mxy )[[1]] ), eval( formals( mxy )[[2]] ) )
[1] 36
>

The problem is "confusion" about whether the "x" in the second argument expression refers to the first argument, or the environment variable. When the function is evaluated, the argument value of x is used, but when the argument is evaluated (using eval and formals) the environment value of x is used. This is a reasonable choice, and mixing up arguments and environment variables in a function definition probably should be considered bad programming.

> Date: Wed, 26 Dec 2007 17:21:46 -0500
> From: ggrothendieck_at_gmail.com
> To: topkatz_at_msn.com
> Subject: Re: [R] Can you recover default argument values of a function?
> CC: r-help_at_stat.math.ethz.ch
>
> If the default value is a constant such as in
>
> f <- function(x = 1) x
>
> then formals(f)[[1]] will give it to you but it could be an expression
> referring to other variables (other arguments, other variables in
> the function, free variables) such as
>
> f <- function(x = u, u) x
>
> or
>
> f <- function(x = u+v+w, u) { v <- u+1; x }
>
> in which case you would get an object of class
> "name" in the first case or "call" in the second.
>
>
> On Dec 26, 2007 4:42 PM, Talbot Katz wrote:
>>
>> Hi.
>>
>> Maybe this is a stupid question. If so, I apologize, but here goes. Suppose I have a function f1(x,...) that calls a function f2(y1,y2,...,yn) in the following way: if x satisfies a certain condition, then I want to call f2(x,y2,...,yn); otherwise I want to use the default value of y1, if there is one. I could do something like the following:
>>
>> v <- ifelse ( is.null(x), f2( , y2,..., yn), f2( x, y2,..., yn) )
>>
>> but I'm doing this in a loop (where the y2,...,yn variables may change), and I'd prefer not to execute the ifelse statement each time, so I'd like an initial pre-loop ifelse such as the following:
>>
>> y0 <- ifelse ( is.null(x), default(y1), x )
>>
>> where default(y1) is the default value of the y1 argument of f2. Then, inside the loop I'd have
>>
>> v <- f2( y0, y2,..., yn )
>>
>> Is there any mechanism that tells me how many arguments a function has, and the default values of each one, if there are default values?
>>
>> Thanks!
>>
>> -- TMK --212-460-5430 home917-656-5351 cell
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>



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 Wed 26 Dec 2007 - 23:12:43 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 Thu 27 Dec 2007 - 09:30:21 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.