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

2012-01-08 Thread William Dunlap
ftware wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf Of Rolf Turner > Sent: Saturday, January 07, 2012 2:07 PM > To: Carl Witthoft > Cc: r-help@r-project.org > Subject: Re: [R] Putt

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 wrote: > On 12-01-07 2:44 PM, cbe...@tajo.ucsd.edu wrote: >> >> Duncan Murdoch  writes: >> >>> On 12-01-06 10:21 PM, Rolf Turner wrote: On 07/01/12 15:51, R. Michael Weylandt wrote: > > I imagine the answer will involve lazy eva

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 Murdoch writes: On 12-01-06 10:21 PM, Rolf Turner wrote: On 07/01/12 15:51, R. Michael Weylandt 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 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 b

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: 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 = i

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 them

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

2012-01-07 Thread cberry
Duncan Murdoch writes: > On 12-01-06 10:21 PM, Rolf Turner wrote: >> On 07/01/12 15:51, R. Michael Weylandt 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

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 "Pendeo

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

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 wrote: > On 12-01-06 10:21 PM, Rolf Turner wrote: >> >> On 07/01/12 15:51, R. Michael Weylandt  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 com

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 Weylandt 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

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 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 > force() in the internal co

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 thi

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 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 <- vector("li

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 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) { junk[[i]] <- eval(bq

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 > 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 1:4) {force(i) junk[[i]] <

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

2012-01-06 Thread R. Michael Weylandt
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 wrote: > > I want to create a list of functions in a for loop, with the index > of the loop ap

[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]] <- eval(bquote