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); }