[R] converting strings to expressions

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Sep 14 21:48:13 CEST 2006


On 14 Sep 2006 21:44:01 +0200, Peter Dalgaard <p.dalgaard at biostat.ku.dk> wrote:
> "Deepayan Sarkar" <deepayan.sarkar at gmail.com> writes:
>
> > Hi,
> >
> > consider this:
> >
> > --------------
> >
> > estr <- c("2^4", "alpha[1]")
> > eexp <- expression(2^4, alpha[1])
> >
> >
> > ## Is it possible to get 'eexp' starting from 'estr'? The closest I could
> > ## get was:
> >
> > do.call(expression, lapply(estr, as.name))
> >
> > ## but it is not quite the same; e.g. the following behave differently:
>
> Er, how about
>
> > estr <- c("2^4", "alpha[1]")
> > parse(text=estr)
> expression(2^4, alpha[1])
>
> or (brain teaser alert!)
>
> > parse(text=deparse(parse(text=estr)))[[1]]
> expression(2^4, alpha[1])
>
> which is _not_ quite the same thing.

Ah, I'd forgotten about parse. A link from ?expression might be reasonable.

Thanks,
Deepayan



More information about the R-help mailing list