From ligges at statistik.tu-dortmund.de  Wed Oct 14 00:19:26 2015
From: ligges at statistik.tu-dortmund.de (Uwe Ligges)
Date: Wed, 14 Oct 2015 00:19:26 +0200
Subject: [Rd] A where() functions that does what exists() does but
 return the environment when object lives?
In-Reply-To: <CAFDcVCRdy-M-2UMn6Q8UB=CpqhonzMAUavvj_aAND9vvqROkuw@mail.gmail.com>
References: <CAFDcVCRR1YwaC=yQXgvgKpoW1HKLPauKYGrU8g9S3q9rAbctLQ@mail.gmail.com>
	<5618CB38.7010606@statistik.tu-dortmund.de>
	<CAFDcVCRdy-M-2UMn6Q8UB=CpqhonzMAUavvj_aAND9vvqROkuw@mail.gmail.com>
Message-ID: <561D836E.7090807@statistik.tu-dortmund.de>



On 13.10.2015 22:39, Henrik Bengtsson wrote:
> 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.

Ah, in the same order.... Then you probably need to write some code, but 
sources of the existing functions should allow for a good start, I believe.

Good luck,
Uwe

>
> 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



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