[R] Help using expression

Prof Brian D Ripley ripley at stats.ox.ac.uk
Wed Aug 9 07:48:37 CEST 2000

On Wed, 9 Aug 2000, Anders Nielsen wrote:

> Dear R users
> I am doing a series of plots and I want the titles to be
> expressions controlled by a loop. I believe the problem
> can be isolated to:
> The plot I want can be produced with:
> plot(5, main=expression(alpha[1992]))
> But why can't I do something this?
> i<-1992
> plot(5, main=expression(paste("alpha[",i,"]",sep="")))

paste("alpha[",i,"]",sep="") is the character string "alpha[1992]".
You need to parse it to make an expression.

> parse(text=paste("alpha[",i,"]",sep=""))
> plot(5, main=parse(text=paste("alpha[",i,"]",sep="")))


There is another way to do this using substitute, which here is simpler.
My first attempt would have been

> plot(5, main=substitute(alpha[i], list(i=i)))

and that works, as the result of substitute is of mode "call" (and type

> substitute(alpha[i], list(i=i))
> mode(.Last.value)
[1] "call"

The two techniques are complementary, sometimes one being easier than the
other.  I tend to use parse in complicated situations as handling
character strings can be easier to debug.

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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list