Question

Put it differently: How can I get the name of a function's environment (not its enclosing environment).

I am trying to assign the result of a function to a certain name inside a function. The name should not occur in the GlobalEnv. Still I do care about the name, because I process deparse(substitute(x)) and therefore need to make sense of the name.

At the moment I just create an environment inside the function, which I feel is kind of a work around:

createList <- function(...,nm="nameString"){
myList <- list(...)
e1 <- new.env()
assign(nm,myList,envir=e1)
# obviously this does not make much sense
# just want to show that the envir thing works
e1[[nm]]
}

Is there any way to do this easier? Not the result, but assigning variable to a certain name inside a function. environment(createList) just gives my back .GlobalEnv, the environment in which the function itself resides, but I want to know how the environment is called that variables inside the function live in. .Private?

Was it helpful?

Solution

Is environment() what you want?

createList <- function(...,nm="nameString"){
  myList <- list(...)
  assign(nm,myList,envir=environment())
  get(nm)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top