From ggrothendieck at gmail.com  Fri May 14 17:10:23 2010
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Fri, 14 May 2010 11:10:23 -0400
Subject: [Rd] Restrict access to variables in parent environment
In-Reply-To: <AANLkTinyl2u3qXHjXcoBbWhBg8YaWlF917j_fEgj-hhO@mail.gmail.com>
References: <AANLkTine-6pPbbOKspjOxOFF7qYC56KItUn_apBUKSgy@mail.gmail.com> 
	<AANLkTinVN5_uVkOaryuBnmw5JTYWdqWRojFRdG41hC-7@mail.gmail.com> 
	<AANLkTinyl2u3qXHjXcoBbWhBg8YaWlF917j_fEgj-hhO@mail.gmail.com>
Message-ID: <AANLkTilP5SFqjCrnvedxO5da-QHqxQxZpvU3QpETANUr@mail.gmail.com>

You can get around that by using this instead:

environment(f) <- as.environment(2)

provided it is done after you have loaded all your packages.

On Fri, May 14, 2010 at 10:29 AM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
> This isn't like a local variable though, since any function above the
> baseenv() in the search path will also not be found.
>
>> f
> function(a) { rnorm(b) }
> <environment: base>
>
>> f()
> Error in f() : could not find function "rnorm"
>
> Jeff
>
> On Fri, May 14, 2010 at 9:20 AM, Hadley Wickham <hadley at rice.edu> wrote:
>
>> > Hello All,
>> >
>> > By default, a reference of a variable in a function cause R to look
>> > for the variable in the parent environment if it is not available in
>> > the current environment (without generating any errors or warnings).
>> > I'm wondering if there is a way to revert this behaviors, such that it
>> > will not look for the parent environment and will generate an error if
>> > the variable is not available in the current environment. Is this
>> > tuning has to be done at the C level?
>>
>> Try this:
>> b <- 1
>> f <- function(a) {
>> ?b
>> }
>>
>> environment(f) <- baseenv()
>> f()
>>
>> Hadley
>>
>> --
>> Assistant Professor / Dobelman Family Junior Chair
>> Department of Statistics / Rice University
>> http://had.co.nz/
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
>
>
> --
> Jeffrey Ryan
> jeffrey.ryan at insightalgo.com
>
> ia: insight algorithmics
> www.insightalgo.com
>
> ? ? ? ?[[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] Restrict access to variables in parent environment
Gabor Grothendieck