Re: [Rd] Restrict access to variables in parent environment

2010-05-14 Thread Jeff Ryan
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 had...@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@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel




-- 
Jeffrey Ryan
jeffrey.r...@insightalgo.com

ia: insight algorithmics
www.insightalgo.com

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Restrict access to variables in parent environment

2010-05-14 Thread Gabor Grothendieck
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.r...@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 had...@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@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel




 --
 Jeffrey Ryan
 jeffrey.r...@insightalgo.com

 ia: insight algorithmics
 www.insightalgo.com

        [[alternative HTML version deleted]]

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel


__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Restrict access to variables in parent environment

2010-05-14 Thread Hadley Wickham
On Fri, May 14, 2010 at 10:10 AM, Gabor Grothendieck
ggrothendi...@gmail.com wrote:
 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.

Another approach is to inspect function by function:

library(codetools)
findGlobals(f, F)$variables

Hadley
-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Restrict access to variables in parent environment

2010-05-14 Thread Simon Urbanek

On May 14, 2010, at 10:29 AM, Jeff Ryan wrote:

 This isn't like a local variable though, since any function above the
 baseenv() in the search path will also not be found.
 

Yes, but that is a consequence of the request and hence intended. You can 
always either specify the full path to the function or assign locally any 
functions you'll need:

 f = function(a) stats::rnorm(b)
 environment(f) - baseenv()
 f()
Error in stats::rnorm(b) : object 'b' not found

 f = function(a) { rnorm=stats::rnorm; rnorm(b) }
 environment(f) - baseenv()
 f()
Error in rnorm(b) : object 'b' not found

Alternatively you can use any environment up the search path hoping that those 
won't define further variables.

But back to the original question -- if taken literally, it's quite useless 
since it corresponds to using emptyenv() instead of baseenv() which means that 
you can't do anything:

 local(1+1,emptyenv())
Error in eval(expr, envir, enclos) : could not find function +


So likely the only useful(?) distinction would be to allow function lookup the 
regular way but change only the variable lookup to not look beyond the current 
environment. That is stretching it, though...

Cheers,
Simon





 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 had...@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@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel
 
 
 
 
 -- 
 Jeffrey Ryan
 jeffrey.r...@insightalgo.com
 
 ia: insight algorithmics
 www.insightalgo.com
 
   [[alternative HTML version deleted]]
 
 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel
 
 

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Restrict access to variables in parent environment

2010-05-14 Thread thmsfuller...@gmail.com
On Fri, May 14, 2010 at 10:13 AM, Simon Urbanek
simon.urba...@r-project.org wrote:

 On May 14, 2010, at 10:29 AM, Jeff Ryan wrote:

 This isn't like a local variable though, since any function above the
 baseenv() in the search path will also not be found.


 Yes, but that is a consequence of the request and hence intended. You can 
 always either specify the full path to the function or assign locally any 
 functions you'll need:

 f = function(a) stats::rnorm(b)
 environment(f) - baseenv()
 f()
 Error in stats::rnorm(b) : object 'b' not found

 f = function(a) { rnorm=stats::rnorm; rnorm(b) }
 environment(f) - baseenv()
 f()
 Error in rnorm(b) : object 'b' not found

 Alternatively you can use any environment up the search path hoping that 
 those won't define further variables.

 But back to the original question -- if taken literally, it's quite useless 
 since it corresponds to using emptyenv() instead of baseenv() which means 
 that you can't do anything:

 local(1+1,emptyenv())
 Error in eval(expr, envir, enclos) : could not find function +


 So likely the only useful(?) distinction would be to allow function lookup 
 the regular way but change only the variable lookup to not look beyond the 
 current environment. That is stretching it, though...

I didn't think of the corner cases. I'm OK with stats::rnorm or
stats:::rnorm. But '{' is not recognized. How to make it be
recognized?

b=1
f=function() {
  stats::rnorm(b)
}
f()
#environment(f)=base()
environment(f)=emptyenv()
f()
 Cheers,
 Simon





 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 had...@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@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel




 --
 Jeffrey Ryan
 jeffrey.r...@insightalgo.com

 ia: insight algorithmics
 www.insightalgo.com

       [[alternative HTML version deleted]]

 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel



 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel




-- 
Tom

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Restrict access to variables in parent environment

2010-05-14 Thread Simon Urbanek

On May 14, 2010, at 11:33 AM, thmsfuller...@gmail.com wrote:

 On Fri, May 14, 2010 at 10:13 AM, Simon Urbanek
 simon.urba...@r-project.org wrote:
 
 On May 14, 2010, at 10:29 AM, Jeff Ryan wrote:
 
 This isn't like a local variable though, since any function above the
 baseenv() in the search path will also not be found.
 
 
 Yes, but that is a consequence of the request and hence intended. You can 
 always either specify the full path to the function or assign locally any 
 functions you'll need:
 
 f = function(a) stats::rnorm(b)
 environment(f) - baseenv()
 f()
 Error in stats::rnorm(b) : object 'b' not found
 
 f = function(a) { rnorm=stats::rnorm; rnorm(b) }
 environment(f) - baseenv()
 f()
 Error in rnorm(b) : object 'b' not found
 
 Alternatively you can use any environment up the search path hoping that 
 those won't define further variables.
 
 But back to the original question -- if taken literally, it's quite useless 
 since it corresponds to using emptyenv() instead of baseenv() which means 
 that you can't do anything:
 
 local(1+1,emptyenv())
 Error in eval(expr, envir, enclos) : could not find function +
 
 
 So likely the only useful(?) distinction would be to allow function lookup 
 the regular way but change only the variable lookup to not look beyond the 
 current environment. That is stretching it, though...
 
 I didn't think of the corner cases. I'm OK with stats::rnorm or
 stats:::rnorm. But '{' is not recognized. How to make it be
 recognized?
 

You either include base or (if you don't want to) you have to create functions 
for everything you'll need including `{` so for example:

 f=function(a) base::rnorm(b)
 environment(f)=mini.env
 mini.env=new.env(parent=emptyenv())
 import=c({,::,+,rnorm) # add all functions you want to use
 for (fn in import) mini.env[[fn]] = get(fn)

 f=function(a) rnorm(a+b)
 environment(f)=mini.env

 f(1)
Error in rnorm(a + b) : object 'b' not found


Cheers,
Simon



 b=1
 f=function() {
  stats::rnorm(b)
 }
 f()
 #environment(f)=base()
 environment(f)=emptyenv()
 f()
 Cheers,
 Simon
 
 
 
 
 
 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 had...@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@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel
 
 
 
 
 --
 Jeffrey Ryan
 jeffrey.r...@insightalgo.com
 
 ia: insight algorithmics
 www.insightalgo.com
 
   [[alternative HTML version deleted]]
 
 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel
 
 
 
 __
 R-devel@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-devel
 
 
 
 
 -- 
 Tom
 
 

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Restrict access to variables in parent environment

2010-05-14 Thread Duncan Murdoch

On 14/05/2010 10:14 AM, thmsfuller...@gmail.com 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?


You could do that by setting the environment of the function to 
emptyenv(), but it will not be pretty.  Remember that everything in R is 
an object, so you won't have access to the base level objects like +, or 
mean, or any other function.


Duncan Murdoch

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel