Yes, that is what I was referring to. In fact my second example shows it can be even worse than in your example since v is computed by f so you can't get v's value without running f.
On Dec 26, 2007 6:09 PM, Talbot Katz <[EMAIL PROTECTED]> wrote: > > 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. > > -- TMK -- > 212-460-5430 home > 917-656-5351 cell > > > > Date: Wed, 26 Dec 2007 17:21:46 -0500 > > From: [EMAIL PROTECTED] > > To: [EMAIL PROTECTED] > > Subject: Re: [R] Can you recover default argument values of a function? > > CC: [EMAIL PROTECTED] > > > > 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@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@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.