(With trepidation): Evaluating expressions with sub expressions again
Thankyou Thomas. Yes, I was motivated by bquote(). It is sooooo slick (to some extent, still too slick for me). I wanted to do it nonrecursively because it's both clearer (I think) and maybe even more efficient (though I doubt this is detectable in any case). Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: Thomas Lumley [mailto:tlumley at u.washington.edu] Sent: Sunday, January 31, 2010 2:57 PM To: Bert Gunter Cc: 'Gabor Grothendieck'; 'Jennifer Young'; r-help at r-project.org Subject: Re: [R] (With trepidation): Evaluating expressions with sub expressions again
On Sun, 31 Jan 2010, Bert Gunter wrote:
(For R language geeks only) Folks: I think the best solution for the issue in the Subject line(see 29 January thread on this for details) was the one Jennifer and Gabor previously arrived at: (essentially)don't use R; instead, use a computer algebra
system
that you can access through an R interface (e.g. Ryacas). HOWEVER, I still wondered whether one could come up with a "simple" pure R solution for simple but more general cases. What I offered previously was too simple for the general case, as Gabor pointed out. It could do: substitute the expression, "1/t" for "b" in the expression a*b; but it could not descend further to handle: substitute the expression, "1/t" for "b" in the expression a*b, where the expression "sin(z+3)" is in turn to be substituted for t. So I fooled with this a bit further and **think** (with trepidation) I
found
that my previous approach does seem to extend to the general case by just repeatedly processing the expression until done. One can even do this without recursion (as it's tail recursion only) as follows (but see the caveat below):
This is more or less what bquote() does, and it works, recursively, in a few
lines of pure R (it just doesn't solve your particular problem because it
substitutes for .() rather than for b).
-thomas
Thomas Lumley Assoc. Professor, Biostatistics
tlumley at u.washington.edu University of Washington, Seattle