[Rd] What functions are called internally in R to resolve what variable is referred?

2010-05-13 Thread thmsfuller...@gmail.com
Hello All,

If I refer to a variable 'x', 'x' will be searched in the current
frame or the parent frame, or the parent of the parent frame, etc.,
until it is found (or not found at all)? Could you please show me what
code in R source that handles this? Is it in the C code or the R code?

Thanks,
Tom

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


[Rd] Restrict access to variables in parent environment

2010-05-14 Thread thmsfuller...@gmail.com
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?

-- 
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 thmsfuller...@gmail.com
On Fri, May 14, 2010 at 10:13 AM, Simon Urbanek
 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) }
>> 
>>
>>> f()
>> Error in f() : could not find function "rnorm"
>>
>> Jeff
>>
>> On Fri, May 14, 2010 at 9:20 AM, Hadley Wickham  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


[Rd] The parsing of '{' and a function that equal to '{'

2010-05-14 Thread thmsfuller...@gmail.com
Hello All,

I tried the sample code from the help. Although '{' is assigned to
'do', the call syntaxes for 'do' and '{' are not the same ('do' has
','s, but '{' has line breaks). I guess there is a difference in
parsing the code block of 'do' and the code block of '{'. Could you
please let me know some internal details so that I can understand the
differences?

> do=get("{")
> do(x <- 3, y <- 2*x-3, 6-x-y)
[1] 0
> {
+   x <- 3
+   y <- 2*x-3
+   6-x-y
+ }
[1] 0
> x
[1] 3
> y
[1] 3
>

-- 
Tom

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


[Rd] Is it possible to generate an error when a function is reassigned?

2010-05-23 Thread thmsfuller...@gmail.com
Hello All,

length() is a function. If I accidentally assign something else to
length, it will give me some unexpected results.

Although the freedom to redefine any funcion maybe good in sometime,
it may be unwanted in many other cases. Is there a way to forbidden
such changes on functions?

-- 
Tom

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