I do not understand this.

I have the same problem and I tried to reproduce it thusly

Q <- 3
test <- function(q=Q){
  cat(q, "\n")
}

> test()
3

Q <- 3
test <- function(q=Q){
  test2(q, "\n")
}

test2 <- function(q, a){
  cat(q, a)
}
> test()
3

Q <- 3
test <- function(q=Q){
  z <- paste(q, "\n")
  test2(z, cat)
}

test2 <- function(q, F){
  F(q)
}
> test()
3


Surly these should have the same problem?

I really do not understand the following explanation, but the get("..",
envir=environment("ThisFunc")) works nicely, but I do not know why.

I thought if I could write a toy programme to reproduce it...

cheers
Worik

"One of the most important things to know about the evaluation of

> arguments to a function is
> that supplied arguments and default arguments are treated differently.
> The supplied arguments
> to a function are evaluated in the evaluation frame of the calling
> function. The default arguments
> to a function are evaluated in the evaluation frame of the function."
>
> the parameter testparams, when no matching argument is passed, is given
> the default value which is the value of the variable testparams
> looked-up *not* in the environment where foo is defined, and *not* in
> the environment where foo is called, but rather in the local environment
> created when the function is called and where parameters are mapped to
> values -- and in this environment, testparams is a parameter, which is
> already being under evaluation, hence the recursive lookup error.
>
> in some programming languages, testparams (the default value) would come
> from the definition-time environment, so you'd not have problems; in
> others, it's more like in r:
>
> python> x = 1; def foo(x, y=x): return y; foo(2) # 1
> ruby> x = 1; def foo(x, y=x); y; end; foo(2) # 2
>
> you just have to get used to it.
>
>
>
>
> > I can obviously rename testparams but I would still like to understand
> why
> > this breaks. I would have thought the right hand side of
> > "testparams=testparams" would have been evaluated in the namespace
> outside
> > the script and not the function. How can I force it to be evaluated in
> the
> > namespace outside the function ?
> >
>
> something like this ugly hack seems to solve the problem:
>
> foo = function(testparams = get("testparams", envir=environment(foo))
>
>
> vQ
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]

