Using R, how to get the environment object from the environment string?

Viewed 24

Let's say I create a function:

x = function() { print(environmentName(environment())); envir=environment(); print(envir); print(environmentName(envir)); print(environmentName(environment())); print(environmentName(as.environment(environment()))); print(environmentName(parent.env(environment()))); envir; }

On my computer it prints out a unique hash of the environment in only one instance. That leads to a fundamental question about what is an envir and how the function environmentName(envir) actually works.

For example,

( env = x() );

outputs:

> ( env = x() );
[1] ""
<environment: 0x000001fec97c3eb0>
[1] ""
[1] ""
[1] ""
[1] "R_GlobalEnv"
<environment: 0x000001fec97c3eb0>

and reports an "" EMPTY environment name:

> environmentName(env)
[1] ""

When I was expecting a string "0x000001fec97c3eb0" or something. I guess every time I call the function x I get a new environment.

> environmentName(x)
[1] ""
> environmentName(environment(x))
[1] "R_GlobalEnv"

New environments

Maybe unless it is "FIXED" to a package or attached as an environment? From (How to get environment of a variable in R)

a <- new.env(parent=emptyenv())
a$x <- 3
attach(a)
b <- new.env(parent=emptyenv())
b$x <- 4

This yields:

environmentName(a);
environmentName(b);

both EMPTY "" ???

It does seem to work on the following, called from GLOBAL:

> environmentName(environment());
[1] "R_GlobalEnv"

So there appears to be a fundamental question:

Fundament Question: How exactly does environmentName work?

But that was not my primary question. The above example is a function that has an envir as an input and returns a string. How can I reverse that process. That is, if I have a stringname R_GlobalEnv how do I return the envir R object?

Primary Question: Is there a function that reverses the logic of environmentName?

That is, if I have a stringname of the environmentName, what function do I call to reverse it? Or what function can be written to enhance the R experience?

e.g.,

env.toName = function(envir=environment()) { environmentName(envir); }
env.fromName = function(envirstr) { XXVXX(envirstr); }
1 Answers

Not all environments have names, which is why you see "" sometimes. environmentName(env) just returns the "name" attribute of env.

There is no standard function that can take the name and return the environment: to write one, you'd have to search all variables in view for the environments, then individually check for a name on each. You might find more than one with the same name attribute, e.g.

a <- new.env()
attr(a, "name") <- "myenv"
b <- new.env()
attr(b, "name") <- "myenv"
environmentName(a)
#> [1] "myenv"
environmentName(b)
#> [1] "myenv"

Created on 2022-09-17 with reprex v2.0.2

Names on environments are there just as decoration, not for unique identification. Check for equality using identical():

d <- a
environmentName(d)
#> [1] "myenv"
identical(a, b)
#> [1] FALSE
identical(a, d)
#> [1] TRUE
Related