Henrik Bengtsson
2015-Oct-09 23:18 UTC
[Rd] A where() functions that does what exists() does but return the environment when object lives?
Hi, exists("foo", inherits=TRUE) check whether an object named "foo" exists, and get("foo", inherits=TRUE) retrieves it. I'm looking for a similar function to exists() that returns the environment where the object "foo" exists, iff at all. If not found, NULL is returned. Does that exist? EXAMPLE #1:> sample2 <- base::sample > env <- where("sample2", inherits=TRUE) > env<environment: R_GlobalEnv> Note the difference to:> obj <- get("sample2", inherits=TRUE) > environment(obj)<environment: namespace:base> EXAMPLE #2:> a <- 1 > foo <- function() { b <- 2; list(a=where("a", inherits=TRUE), b=where("b", inherits=TRUE)) } > foo()$a <environment: R_GlobalEnv> $b <environment: 0x000000000b7d2798>> foo()$a <environment: R_GlobalEnv> $b <environment: 0x000000000b7d23e0> I do understand that I can implement such a function myself, but I prefer not to. Thanks, Henrik
Uwe Ligges
2015-Oct-10 08:24 UTC
[Rd] A where() functions that does what exists() does but return the environment when object lives?
I'd start looking at getAnywhere(). Best, Uwe On 10.10.2015 01:18, Henrik Bengtsson wrote:> Hi, > > exists("foo", inherits=TRUE) check whether an object named "foo" > exists, and get("foo", inherits=TRUE) retrieves it. I'm looking for a > similar function to exists() that returns the environment where the > object "foo" exists, iff at all. If not found, NULL is returned. > Does that exist? > > EXAMPLE #1: > >> sample2 <- base::sample >> env <- where("sample2", inherits=TRUE) >> env > <environment: R_GlobalEnv> > > Note the difference to: > >> obj <- get("sample2", inherits=TRUE) >> environment(obj) > <environment: namespace:base> > > > EXAMPLE #2: > >> a <- 1 >> foo <- function() { b <- 2; list(a=where("a", inherits=TRUE), b=where("b", inherits=TRUE)) } >> foo() > $a > <environment: R_GlobalEnv> > $b > <environment: 0x000000000b7d2798> > >> foo() > $a > <environment: R_GlobalEnv> > $b > <environment: 0x000000000b7d23e0> > > > I do understand that I can implement such a function myself, but I > prefer not to. > > Thanks, > > Henrik > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Henrik Bengtsson
2015-Oct-13 20:39 UTC
[Rd] A where() functions that does what exists() does but return the environment when object lives?
On Sat, Oct 10, 2015 at 1:24 AM, Uwe Ligges <ligges at statistik.tu-dortmund.de> wrote:> I'd start looking at getAnywhere().Thanks Uwe, that does indeed provides "where" information. Unfortunately, I don't see how it will allow me to search environments similarly/in the same order as exists/get(..., envir, inherits=TRUE) does it. getAnywhere() will search everything in any order. It's a start though. Cheers, Henrik> > Best, > Uwe > > > On 10.10.2015 01:18, Henrik Bengtsson wrote: >> >> Hi, >> >> exists("foo", inherits=TRUE) check whether an object named "foo" >> exists, and get("foo", inherits=TRUE) retrieves it. I'm looking for a >> similar function to exists() that returns the environment where the >> object "foo" exists, iff at all. If not found, NULL is returned. >> Does that exist? >> >> EXAMPLE #1: >> >>> sample2 <- base::sample >>> env <- where("sample2", inherits=TRUE) >>> env >> >> <environment: R_GlobalEnv> >> >> Note the difference to: >> >>> obj <- get("sample2", inherits=TRUE) >>> environment(obj) >> >> <environment: namespace:base> >> >> >> EXAMPLE #2: >> >>> a <- 1 >>> foo <- function() { b <- 2; list(a=where("a", inherits=TRUE), >>> b=where("b", inherits=TRUE)) } >>> foo() >> >> $a >> <environment: R_GlobalEnv> >> $b >> <environment: 0x000000000b7d2798> >> >>> foo() >> >> $a >> <environment: R_GlobalEnv> >> $b >> <environment: 0x000000000b7d23e0> >> >> >> I do understand that I can implement such a function myself, but I >> prefer not to. >> >> Thanks, >> >> Henrik >> >> ______________________________________________ >> R-devel at r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-devel >> > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Hadley Wickham
2015-Oct-13 21:43 UTC
[Rd] A where() functions that does what exists() does but return the environment when object lives?
Seems easy enough to write yourself: where <- function(x, env = parent.frame()) { if (identical(env, emptyenv())) return(NULL) if (exists(x, envir = env, inherits = FALSE)) return(env) where(x, parent.env(env)) } sample2 <- base::sample where("sample2") #> <environment: 0x1154d3c28> where("sample") #> <environment: base> where("blah") #> NULL Hadley On Fri, Oct 9, 2015 at 6:18 PM, Henrik Bengtsson <henrik.bengtsson at ucsf.edu> wrote:> Hi, > > exists("foo", inherits=TRUE) check whether an object named "foo" > exists, and get("foo", inherits=TRUE) retrieves it. I'm looking for a > similar function to exists() that returns the environment where the > object "foo" exists, iff at all. If not found, NULL is returned. > Does that exist? > > EXAMPLE #1: > >> sample2 <- base::sample >> env <- where("sample2", inherits=TRUE) >> env > <environment: R_GlobalEnv> > > Note the difference to: > >> obj <- get("sample2", inherits=TRUE) >> environment(obj) > <environment: namespace:base> > > > EXAMPLE #2: > >> a <- 1 >> foo <- function() { b <- 2; list(a=where("a", inherits=TRUE), b=where("b", inherits=TRUE)) } >> foo() > $a > <environment: R_GlobalEnv> > $b > <environment: 0x000000000b7d2798> > >> foo() > $a > <environment: R_GlobalEnv> > $b > <environment: 0x000000000b7d23e0> > > > I do understand that I can implement such a function myself, but I > prefer not to. > > Thanks, > > Henrik > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel-- http://had.co.nz/
Hadley Wickham
2015-Oct-13 21:44 UTC
[Rd] A where() functions that does what exists() does but return the environment when object lives?
On Tue, Oct 13, 2015 at 4:43 PM, Hadley Wickham <h.wickham at gmail.com> wrote:> Seems easy enough to write yourself: > > where <- function(x, env = parent.frame()) { > if (identical(env, emptyenv())) > return(NULL) > if (exists(x, envir = env, inherits = FALSE)) > return(env) > where(x, parent.env(env)) > } > > sample2 <- base::sample > where("sample2") > #> <environment: 0x1154d3c28>And that returns a random environment because I ran it with reprex::reprex(). In interactive use it will return <environment: R_GlobalEnv> Hadley -- http://had.co.nz/
Possibly Parallel Threads
- A where() functions that does what exists() does but return the environment when object lives?
- A where() functions that does what exists() does but return the environment when object lives?
- A where() functions that does what exists() does but return the environment when object lives?
- topenv of emptyenv
- apply, t-test and p-values