Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-08 Thread Duncan Murdoch
On 12-01-07 2:44 PM, cbe...@tajo.ucsd.edu wrote: Duncan Murdochmurdoch.dun...@gmail.com writes: On 12-01-06 10:21 PM, Rolf Turner wrote: On 07/01/12 15:51, R. Michael Weylandtmichael.weyla...@gmail.com wrote: I imagine the answer will involve lazy evaluation and require you use force()

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-08 Thread Gabor Grothendieck
On Sun, Jan 8, 2012 at 7:50 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 12-01-07 2:44 PM, cbe...@tajo.ucsd.edu wrote: Duncan Murdochmurdoch.dun...@gmail.com  writes: On 12-01-06 10:21 PM, Rolf Turner wrote: On 07/01/12 15:51, R. Michael Weylandtmichael.weyla...@gmail.com wrote:

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-08 Thread William Dunlap
The following gives a list, 'z', of functions with different values of the variable 'i': z - lapply(1:3, function(i) { force(i) ; function() cat(i is, i, \n)}) z[[3]]() i is 3 z[[2]]() i is 2 Their printed values don't show the difference, as they depend on the 'i' stored in their

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-07 Thread Duncan Murdoch
On 12-01-06 10:21 PM, Rolf Turner wrote: On 07/01/12 15:51, R. Michael Weylandtmichael.weyla...@gmail.com wrote: I imagine the answer will involve lazy evaluation and require you use force() but I'm not quite qualified to pronounce and not at a computer to test. I think you've got it; I

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-07 Thread Gabor Grothendieck
On Sat, Jan 7, 2012 at 10:23 AM, Duncan Murdoch murdoch.dun...@gmail.com wrote: On 12-01-06 10:21 PM, Rolf Turner wrote: On 07/01/12 15:51, R. Michael Weylandtmichael.weyla...@gmail.com  wrote: I imagine the answer will involve lazy evaluation and require you use force() but I'm not quite

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-07 Thread jim holtman
Here is yet another way of doing it using 'local': junk- vector(list,4) for(i in 1:4) { + junk[[i]] - local({ + local_i - i + function(x) 42 + local_i * x + }) + } for (i in 1:4) cat(i, junk[[i]](1), '\n') 1 43 2 44 3 45 4 46 On Sat, Jan 7, 2012 at 10:38 AM, Gabor

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-07 Thread Carl Witthoft
Now that we've all satisfied our curiosity :-) about force() in for and while loops, I suppose it would be impolite to ask Rolf whether there isn't a much neater and simpler way to make his internal functions grab whatever the index 'i' is pointing them to? -- Sent from my Cray XK6

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-07 Thread cberry
Duncan Murdoch murdoch.dun...@gmail.com writes: On 12-01-06 10:21 PM, Rolf Turner wrote: On 07/01/12 15:51, R. Michael Weylandtmichael.weyla...@gmail.com wrote: I imagine the answer will involve lazy evaluation and require you use force() but I'm not quite qualified to pronounce and not at

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-07 Thread Rolf Turner
On 08/01/12 05:24, Carl Witthoft wrote: Now that we've all satisfied our curiosity :-) about force() in for and while loops, I suppose it would be impolite to ask Rolf whether there isn't a much neater and simpler way to make his internal functions grab whatever the index 'i' is pointing

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-07 Thread Rolf Turner
On 08/01/12 04:38, Gabor Grothendieck wrote: SNIP These two variations without bquote and the third which just replaces for with while all (that I had previously posted) do work: # 1 junk- vector(list,4) for(i in 1:4) { junk[[i]]- eval(substitute(function(x) { 42 + i * x }, list(i =

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-07 Thread peter dalgaard
On Jan 7, 2012, at 23:29 , Rolf Turner wrote: The fact that I don't really understand any of this stuff and am basically groping around in the dark, hammering and hoping, doesn't make it any easier! :-) As if it weren't confusing enough when working as intended, there appears to have been

[R] Putting an index explicitly into function code --- a curiosity.

2012-01-06 Thread Rolf Turner
I want to create a list of functions in a for loop, with the index of the loop appearing explicitly in the function code. After quite a bit of thrashing around I figured out how to do it. Here is a toy example: junk - vector(list,4) for(i in 1:4) { itmp - i junk[[i]] -

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-06 Thread R. Michael Weylandt michael.weyla...@gmail.com
I imagine the answer will involve lazy evaluation and require you use force() but I'm not quite qualified to pronounce and not at a computer to test. Michael On Jan 6, 2012, at 8:43 PM, Rolf Turner rolf.tur...@xtra.co.nz wrote: I want to create a list of functions in a for loop, with the

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-06 Thread David Winsemius
On Jan 6, 2012, at 9:51 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: I imagine the answer will involve lazy evaluation and require you use force() but I'm not quite qualified to pronounce and not at a computer to test. Your theory passes the experimental test: for(i in

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-06 Thread Rolf Turner
On 07/01/12 15:51, R. Michael Weylandt michael.weyla...@gmail.com wrote: I imagine the answer will involve lazy evaluation and require you use force() but I'm not quite qualified to pronounce and not at a computer to test. I think you've got it; I tried junk - vector(list,4) for(i in 1:4) {

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-06 Thread Gabor Grothendieck
On Fri, Jan 6, 2012 at 9:43 PM, Rolf Turner rolf.tur...@xtra.co.nz wrote: I want to create a list of functions in a for loop, with the index of the loop appearing explicitly in the function code. After quite a bit of thrashing around I figured out how to do it. Here is a toy example: junk

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-06 Thread R. Michael Weylandt
Presumably because the i = 4 has to be re-evaluated at the start of each iteration of the while-loop which implicitly force()s it? Though, I don't know if it might not be a bad idea to put an implicit force() in the internal code for `for` to prevent these sorts of things. I can't immediately

Re: [R] Putting an index explicitly into function code --- a curiosity.

2012-01-06 Thread Gabor Grothendieck
On Fri, Jan 6, 2012 at 11:13 PM, R. Michael Weylandt michael.weyla...@gmail.com wrote: Presumably because the i = 4 has to be re-evaluated at the start of each iteration of the while-loop which implicitly force()s it? Though, I don't know if it might not be a bad idea to put an implicit