Suppose I have the following function:
g = function(x) x+h
Now, if I have in my environment an object named h
, I would not have any problem:
h = 4
g(2)
## should be 6
Now, I have another function:
f = function() {
h = 3
g(2)
}
I would expect:
rm(h)
f()
## should be 5, isn't it?
Instead, I get an error
## Error in g(2) : object 'h' not found
I would expect g
to be evaluated within the environment of f
, so that the h
in f
will be bound to the h in g, as it was when I executed g
within the .GlobalEnv
. This does not happen (obviously). any explanation why? how to overcome this so that the function within the function(e.g. g
) will be evaluated using the enclosing environment?