Re: [R] Help with using 'get' function and variable scope

2008-04-18 Thread Duncan Murdoch
On 4/18/2008 9:19 AM, Gabor Grothendieck wrote: > On Fri, Apr 18, 2008 at 7:49 AM, Duncan Murdoch <[EMAIL PROTECTED]> wrote: >> On 18/04/2008 7:27 AM, Gabor Grothendieck wrote: >> > If you define your functions in the loop you can it directly >> > since then the scoping rules work in your favor: >>

Re: [R] Help with using 'get' function and variable scope

2008-04-18 Thread Gabor Grothendieck
On Fri, Apr 18, 2008 at 7:49 AM, Duncan Murdoch <[EMAIL PROTECTED]> wrote: > On 18/04/2008 7:27 AM, Gabor Grothendieck wrote: > > If you define your functions in the loop you can it directly > > since then the scoping rules work in your favor: > > > > for(i in 1:4) { > > f <- function() i*i > > p

Re: [R] Help with using 'get' function and variable scope

2008-04-18 Thread Duncan Murdoch
On 18/04/2008 7:27 AM, Gabor Grothendieck wrote: > If you define your functions in the loop you can it directly > since then the scoping rules work in your favor: > > for(i in 1:4) { > f <- function() i*i > print(f()) > } f doesn't need to be in the loop, it just needs to be defined in the s

Re: [R] Help with using 'get' function and variable scope

2008-04-18 Thread Gabor Grothendieck
If you define your functions in the loop you can it directly since then the scoping rules work in your favor: for(i in 1:4) { f <- function() i*i print(f()) } or via lapply: F <- function(i) { f <- function() i*i; print(f()) } lapply(1:4, F) Often the sort of situation you discuss is really

Re: [R] Help with using 'get' function and variable scope

2008-04-18 Thread Peter Waltman
Hi Duncan - Thanks for the reply. Yeah, I understand what I'm doing is a bit weird, but I'm actually calling a few functions w/in the for-loop that need the value of the "i" var, and because I was a bit confused by the concept of environments, I was hoping to avoid having to pass it in as an arg

Re: [R] Help with using 'get' function and variable scope

2008-04-17 Thread Duncan Murdoch
On 17/04/2008 5:37 PM, Peter Waltman wrote: > Hi - > > I'm having a really hard time w/understanding R's get function, and would > appreciate any help with this. > > Specifically, I'm using a for loop to call a function. I'd like the > function to have access to the variable being incremented in

[R] Help with using 'get' function and variable scope

2008-04-17 Thread Peter Waltman
Hi - I'm having a really hard time w/understanding R's get function, and would appreciate any help with this. Specifically, I'm using a for loop to call a function. I'd like the function to have access to the variable being incremented in the for-loop, i.e. t.fn <- function() return( get( "i" )