[R] converting character strings to eval
John Hendrickx
john_hendrickx at yahoo.com
Tue Jul 20 13:19:17 CEST 2004
--- Deepayan Sarkar <deepayan at stat.wisc.edu> wrote:
> On Monday 19 July 2004 09:37, Wayne Jones wrote:
> > Hi there fellow R-users,
> >
> > I'm stuck on this seemingly trivial problem.
> >
> > All I want to coerce a character string into a command.
> >
> > For example:
> >
> > x<-rnorm(20)
> > y<-rnorm(20)
> > str<-"lm(y~x)"
> >
> > I want to evaluate the "str" command.
> >
> > I have tried
> >
> > eval(as.expression(str))
>
> eval(parse(text = str))
>
> seems to work.
>
Couldn't eval be modified to automatically parse arguments if they're
not expressions? Something like:
eval2<-function(arg) {
if (!is.expression(arg)) arg<-parse(text=arg)
eval(arg)
}
Would a construction like eval2 have a downside or cause problems
down the line?
More information about the R-help
mailing list