Hello,
I have difficulties to understand this one:
foo - function (y = 2) {
bar - function (y = y) y^2
bar()
}
foo()
#! Error in y^2 : 'y' is missing
foo(3)
#! Error in y^2 : 'y' is missing
Note that this one works:
foo - function (y = 2) {
bar - function (y = y) y^2
bar(y) #
On 31/03/2014 10:40 AM, Philippe Grosjean wrote:
Hello,
I have difficulties to understand this one:
foo - function (y = 2) {
bar - function (y = y) y^2
bar()
}
foo()
#! Error in y^2 : 'y' is missing
foo(3)
#! Error in y^2 : 'y' is missing
This is simply a misunderstanding about
...@sciviews.org]
Sent: Monday, March 31, 2014 9:40 AM
To: R Help
Subject: [R] Default argument not passed to subfunction when argument name
matches default expression
Hello,
I have difficulties to understand this one:
foo - function (y = 2) {
bar - function (y = y) y^2
bar()
}
foo
Note that the fact that bar() is defined within foo() is irrelevant.
## At the top level/global prompt:
y - 2
bar- function(y=y)y^2
bar()
Error in y^2 : 'y' is missing
## but
bar(y)
[1] 4
This is due to lazy evaluation and promises: The formal argument y
is not evaluated until it's used
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 =
5 matches
Mail list logo