Why i
is different...
It looks like there were changes in R 3.2. An index variable i
has been added to the current environment of lapply
(which is what sapply
actually calls). This goes along with the new behavior to force evaluation of the parameters passed along to the function you are applying over. This means that you now have access to the index of the current iteration you are on in the loop.
The reason fn
and gn
behave differently is that exists()
looks in the environment where it is called. In the case of fn
, that is the environment where this i
variable has been created. In the case of gn
, it's looking in the environment of your anonymous function. When R cannot find a symbol in the local environment, it searches environments based on where functions where defined, not where they are called. This means R will not find the i
variable since your anonymous function is defined in a place where the i
variables does not exist.
We can write a little helper function to make it easier to grab the current index.
idx <- function() get("i", parent.frame(2))
sapply(letters[1:3], function(x) paste(idx(), x))
# a b c
# "1 a" "2 b" "3 c"
As far as I can tell this is currently undocumented behavior. It may change in future versions of R.
Why d
is different...
The discrepancy with the d
variable is a more direct scoping issue. Again R is creating a new environment which it is using to call the function exists
. The parent of this environment is the base environment. So when you call exists
it looks where it was called from (which is this environment where i
exists) and since it doesn't find d
there, it searches the next parent which is the base environment. The current function environment is never searched. You could explicitly search the current environment with
fn <- function (d) {
sapply( vars, exists, where=environment() )
}
fn(d=2)
# a b c d e f g h i j
# TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
For more information on environments in R I suggest you read the Environments section of Advanced R