[R] Default argument not passed to subfunction when argument name matches default expression

Gabor Grothendieck ggrothendieck at gmail.com
Mon Mar 31 20:52:19 CEST 2014


On Mon, Mar 31, 2014 at 11:08 AM, Tierney, Luke <luke-tierney at uiowa.edu> wrote:
> The environment in which default arguments are evaluated is the
> environment of the function call itself, not the environment of the
> caller or the lexical enclosure (the same here). So the two 'y' used
> in function(y = y) refer to the same binding, hence circular
> reference. If you use
>
> foo <- function (z = 2) {
>      bar <- function (y = z) y^2
>     bar()
> }
>

If it were important to keep the same variable name, y, in both places
then this would be a way to avoid the recursive reference:

foo <- function (y = 2) {
     bar <- function (y = parent.frame()$y) y^2
     bar()
}
foo()


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com




More information about the R-help mailing list