0

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); }
Dharman
  • 30,962
  • 25
  • 85
  • 135
mshaffer
  • 959
  • 1
  • 9
  • 19

1 Answers1

0

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
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • It seems like you are saying that the function is just getting the `name` attribute? If that were the case, how do I get the `TRUE` environment name from an environment object `` ... How to extract the `0x000001fec97c3eb0` as a string? The question then would be the reverse. If I had the string `0x000001fec97c3eb0`, could I get the function name? – mshaffer Sep 21 '22 at 14:01
  • There is no such thing as a "true" environment name. What you see is just the way environment objects are printed when they don't have a name, and aren't one of a few special ones. I believe it's the hex value of the C pointer to the environment, but at R level, there is no way to work directly with C pointers. If you write code in C you might be able to use it, but it's very tricky to know whether it remains valid at some future time. Just assign the environment to an R variable, and work with that. – user2554330 Sep 21 '22 at 16:45