[R] Reading expressions from character vectors

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Feb 4 17:30:52 CET 2007


On Sun, 4 Feb 2007, Charilaos Skiadas wrote:

> Greetings,
>
> I have a problem that I am sure is very straightforward, but I just
> can't wrap my head around it. I've read the help pages on text,
> plotmath, expression, substitute, but somehow I can't find the answer
> to this simple question.
>
> Basically consider the following example:
>
> plot( NULL, xlim = c(0,2), ylim = c(0,2) )
> expressions <-  expression( -infinity, infinity )
> text( c(0.5,1.5), 1.5, expressions )
> labels <- c( "-infinity", "infinity" )
> text( c(0.5,1.5), 0.5, as.expression(labels) )
>
> I want the character vector "labels" to be interpreted as  an
> expression vector, and so to appear just like the expressions vector.
> Is this possible? I mean yes, it is probably possible, but how?
>
> I suppose the problem is that the result of as.expression(labels) is
> expression("-infinity", "infinity") instead of expression(-infinity,
> infinity), as I would have liked.  I just can't figure out how to
> convert it to the right thing.

The simplest way is almost certainly parse(text=labels).
It is complicated by the fact that -infinity is a call and infinity is a 
symbol.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list