[R] testing for emptyenv

Duncan Murdoch murdoch.duncan at gmail.com
Thu Sep 2 01:21:05 CEST 2010


On 01/09/2010 7:03 PM, Vadim Ogranovich wrote:
> Dear R-users,
> 
> Is there a way to test whether a particular environment e is equal to emtyenv(), or for that sake whether two environments e1 and e2 are equal?
> 
> I tried a couple of ways to compare environments, but neither seem to work:
> 
>> e1 <- new.env()
>> e2 <- new.env()
>> e1 == e2
> Error in e1 == e2 :
>   comparison (1) is possible only for atomic and list types
>> all.equal(e1, e2)
> [1] TRUE
>> all.equal(e1, emptyenv())
> [1] TRUE
> 
> My ultimate goal is to make a list of all variables in the lexical scope (enclosure?) of a function FUN. So I thought I'd start with environment(FUN) and apply parent.env() until I reach emptyenv(). But for this to work I need to be able to detect that I reached emptyenv().

identical(e1, e2) should give the right answer, assuming you want e1 and 
e2 to test as different.

Duncan Murdoch



More information about the R-help mailing list