[R] (With trepidation): Evaluating expressions with sub expressions again

Thomas Lumley tlumley at u.washington.edu
Sun Jan 31 23:57:29 CET 2010


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



More information about the R-help mailing list