On Mon, Mar 31, 2014 at 11:08 AM, Tierney, Luke <luke-tier...@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

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

Reply via email to