In R, if I create an environment and then use with to evaluate a function in that environment, the function usually has access to the variables. However, if I have a socket function, for some reason they go beyond. Can you explain to me why this is so?
Example:
Create a new environment with the variable x
E = new.env(); E$x = c(1,2,3)
Using with , I can print this variable:
with(E, print(x));
But now, if I nested this function, it no longer works:
printMe = function() { print(x); } with(E, printMe())
I know that I can make it work like this:
printMe = function(x) { print(x); } with(E, printMe(x))
But I don't understand - if with creates an environment, why can't a nested function see x ? It works if you attach it:
attach(E) printMe()
I think I just missed something, but what is the recommended way to do this? Or, to ask your question in another way: why can't nested functions in with access free variables?
source share