[R] Replacement in an expression - can't use parse()

Daniel Berg daniel at danielberg.no
Wed Mar 28 10:09:12 CEST 2007


Thank you very much for your good suggestions.
I have chosen to pursue the suggestion by Peter which worked like a dream :)
However, my problem is slightly more complicated still. I apologize
for not mentioning this in the initial question.

I have to do the evaluation inside a loop, not knowing explicitly
which 'u' to replace. This is given by the loop I'm in, say something
like:

i <- 2; j <- 3
eval(substitute(substitute(call,list(paste("u",i,sep="")=quote(x),paste("u",j,sep="")=1)),list(call=e[[1]])))

But this returns a syntax error.
Any further suggestions?

Regards,
Daniel

On 3/27/07, Thomas Lumley <tlumley at u.washington.edu> wrote:
> On Tue, 27 Mar 2007, Peter Dalgaard wrote:
>
> >The way around this is to add a further layer of substitute() to insert
> >the value of e:
>
> >> eval(substitute(substitute(call,list(u2=quote(x),u3=1)),list(call=e[[1]])))
> > u1 + x + 1
>
> Or eval(do.call(substitute, list(e[[1]], list(u2=quote(x),u3=1)))
>
>         -thomas
>
>


-- 
danielberg.no



More information about the R-help mailing list