Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get environment of a variable in R

I was wondering if there is anyway to get the environment of a declared variable. Say I already have declared a variable to an environment and want to use that variable's environment to declare a few more variables. Something like getEnv("variable")

like image 941
Avinash Avatar asked May 16 '13 09:05

Avinash


People also ask

How do I find global environment in R?

R Programming Environment The top level environment available to us at the R command prompt is the global environment called R_GlobalEnv . Global environment can be referred to as . GlobalEnv in R codes as well. We can use the ls() function to show what variables and functions are defined in the current environment.

Where are R environment variables stored?

These files are set for each version of R and should be located in R_HOME/etc/ . You can find R_HOME by running the command R.

How do I find local environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables.


2 Answers

Refer to: http://adv-r.had.co.nz/Environments.html#env-basics

library(pryr)
x <- 5
where("x")
#> <environment: R_GlobalEnv>
where("mean")
#> <environment: base>

The where function is described in the above website. It only finds the first environment the variable appears in, but could easily be modified to find all.

like image 79
Keith Avatar answered Oct 07 '22 12:10

Keith


You can get all objects in your workspace with ls(), so you can then check which of those are environments:

envirs <- ls()[sapply(ls(), function(x) is.environment(get(x)))]

I need to use get() there because ls() returns character names of objects rather than the objects themselves. Now given some object x, we want to find which environments it exists in. All we need to do is iterate through each environment in envirs, and check if they contain whatever object we're looking for. Something along the lines of (checking for a variable x):

sapply(envirs, function(e) 'x' %in% ls(envir=get(e)))

Here's a function to do all this:

getEnv <- function(x) {
  xobj <- deparse(substitute(x))
  gobjects <- ls(envir=.GlobalEnv)
  envirs <- gobjects[sapply(gobjects, function(x) is.environment(get(x)))]
  envirs <- c('.GlobalEnv', envirs)
  xin <- sapply(envirs, function(e) xobj %in% ls(envir=get(e)))
  envirs[xin] 
}

This is more or less the same as what I did outside the function. gobjects reads from ls(), this time explicitly checking the global environment .GlobalEnv, since it is now within a function.

envirs is the same as before, except now it will check .GlobalEnv as well. xin is storing the names of which environments x was found in. The line:

xobj <- deparse(substitute(x))

Allows object to be tested without quotes e.g. getEnv(x) versus getEnv('x'). That's a matter of preference though, you can change it to accept characters instead.


Here's a few tests.

x1 <- 1
getEnv(x1)
# ".GlobalEnv"

x2 <- 2.1
e2 <- new.env()
assign('x2', 2.2, e2)
getEnv(x2)
# ".GlobalEnv" "e2" 

e3 <- new.env()
assign('x3', 3, e3)
getEnv(x3)
# "e3"

This only checks environments created within .GlobalEnv. I'm sure you can work out how to extend it to search across more environments though if you need.

I'm surprised there isn't some in-built function for this. Or maybe there is and I don't know about it. I've never actually needed to do anything like this before so maybe it's not actually surprising.

like image 36
Ciarán Tobin Avatar answered Oct 07 '22 14:10

Ciarán Tobin