R: Functions -- Display of environment name instead of memory address of that environment?

You can set an environment name using attr, like so:

e <- new.env()
attr(e, "name") <- "xyzzy"

environmentName(e)
## [1] "xyzzy"

If I recall correctly, environment names for packages and namespaces are assigned at the C level. So user-created environments do not reveal names. You cannot set an environment name in R even though there is a (misleadingly named) base function called environmentName(). It will only return the name assigned at C level. It is really only meant for packages and namespaces, not other environments.


Most environments don't have names - the name of an environment is a special attribute of the environment, not the name of the object pointing to that environment. E.g. in the following case, what would you expect the "name" of the environment of f to be?

e1 <- new.env()
e1$z <- 10
e2 <- e1
e3 <- e1

f <- function(x) {
  x + z 
}
environment(f) <- e1

identical(e1, e2)
identical(e1, e3)

Tags:

R