Skip to content
Prev 44755 / 63424 Next

Behavior or as.environment in function arguments/call (and force() behaviors...)

On 13-01-01 4:35 PM, Jeff Ryan wrote:
Even though it is used as a default in a number of places, the pos==-1 
value is really poorly documented.  You need to look in the source, in 
particular src/main/envir.c, function pos2env.  There you'll see that 
pos==-1 is special cased to be the environment from which pos.to.env
(or as.environment in your case) was called.  For non-negative values, 
it indexes the search list (i.e. the list returned by search().)  Other 
values are errors.

The trouble in your examples is that this location varies.  In Fn1, it 
is being called in the ls() call.  In Fn2, it is in the force() call. 
In Fn3 and Fn4, it's the Fn3/Fn4 call.

In spite of what the docs say in ?get, I would rarely if ever use a pos 
argument to as.environment.  Use an environment and pass it as envir.

Duncan Murdoch