From henrik.bengtsson at ucsf.edu  Sat Oct 10 01:18:22 2015
From: henrik.bengtsson at ucsf.edu (Henrik Bengtsson)
Date: Fri, 9 Oct 2015 16:18:22 -0700
Subject: [Rd] A where() functions that does what exists() does but return
 the environment when object lives?
Message-ID: <CAFDcVCRR1YwaC=yQXgvgKpoW1HKLPauKYGrU8g9S3q9rAbctLQ@mail.gmail.com>

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



[Rd] A where() functions that does what exists() does but return the environment when object lives?
Henrik Bengtsson