how to compare environment for equality in R
Asked Answered
E

2

11

I would like to check if the current environment is the global environment in R. However, direct comparison doesn't seem to work with evironments. What is the best way to do this?

#doesn't work
sys.frame() == .GlobalEnv
Excogitate answered 23/1, 2013 at 23:46 Comment(1)
use identical. == is for vector comparison.Lemmons
C
11

Matthew Plourde's solution:

> identical(sys.frame(),.GlobalEnv)
[1] TRUE

You can also check names, though this might be less reliable:

> environmentName(sys.frame())=="R_GlobalEnv"
[1] TRUE
Charlatan answered 23/1, 2013 at 23:50 Comment(1)
and all.equal if you just want to check whether 2 different environments both have the same contentsElla
L
0

Both print and cat return <environment: "an address"> but can't be used for comparison. However format seems to do the job.

Just use format (envir1) == format (envir2)

Lobster answered 24/8, 2023 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.