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

Rolf Turner rolf.turner at xtra.co.nz
Sat Jan 7 04:21:00 CET 2012


On 07/01/12 15:51, R. Michael Weylandt <michael.weylandt at 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) {
    junk[[i]] <- eval(bquote(function(x){42 + .(force(i))*x}))
}

and got the result that I wanted.  Still don't completely understand, but
it at least makes vague sense and makes me a bit more comfy.

Thanks.

     cheers,

         Rolf



More information about the R-help mailing list