On 6/20/20 5:49 PM, Bert Gunter wrote:
Gents:
(with trepidation)
f(x = 3, y = g(expr))
**already** evaluates g in the environment of f, **not** in the
environment of the caller.
(This does not contradict Duncan's example -- 3 is a constant, not a
variable).
e.g.
> f <- function(x = 3, y = x^2 +k){
+ k <- 3
+ x + y
+ }
Ergo
> k <- 100; x <- 10
> f()
[1] 15
> f(0)
[1] 3
> x
[1] 10
This is all due to lazy evaluation where default arguments are
evaluated in the function's environment (using standard evaluation).
Arguments supplied in the call are evaluated in the caller's
environment, so:
> f(x = x)
[1] 113
Am I missing something here?
Cheers,
Bert Gunter
"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
Default arguments are indeed evaluated in f's environment, but not
supplied arguments. I haven't really thought about the semantics of 'g'
with respect to default arguments. But certainly, lazy evaluation is key
here.
Ben (with trepidation as well)
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.