By default, a do.in.envir
function will have, as its enclosing environment, the environment in which it was called, rather than defined. It can therefore read variables in its caller's frame directly (i.e. without using get
), and can assign to them via <<-
. It's also possible to use do.in.envir
to set a completely different enclosing environment; this is exemplified by some of the functions in debug
, such as go
.
Note the difference between do.in.envir
and mlocal
; mlocal
functions evaluate in the frame of their caller (by default), whereas do.in.envir
functions evaluate in their own frame, but have a non-standard enclosing environment defined by the envir
argument.
Calls to e.g. sys.nframe
won't work as expected inside do.in.envir
functions. You need to offset the frame argument by 5, so that sys.parent()
should be replaced by sys.parent( 5)
and sys.call
by sys.call(-5)
.
do.in.envir
functions are awkward inside namespaced packages, because the code in fbody
will have "forgotten" its original environment when it is eventually executed. This means that objects in the namespace will not be found.
The debug package does not yet trace inside do.in.envir
functions-- this will change.