R-beta: Including an evaluated function in text expression

Thomas Lumley thomas at biostat.washington.edu
Fri Mar 27 20:52:06 CET 1998


On Fri, 27 Mar 1998, Matthew R. Nelson wrote:
> I would like to take advantage of R's mathematical typesetting 
> capabilities in the graphical environment using text(..., 
> expression(...)), however, I can't figure out how to include in the 
> expression some evaluated function.  For example,
> 
> > x <- 1:8
> > plot(1:10, 1:10)
> > text(5, 5, expression(sum(x[i], i == 1, length(x)))  # would like "8" 
>                                         # substituted for length(x) here
> 
> Is there a way to replace length(x) with the returned value of the 
> function?

Yes:
	R>text(5,5,parse(text=paste("sum(x[i],i==1,",length(x),")",sep="")))

What we do here is construct a string
	R> paste("sum(x[i],i==1,",length(x),")",sep="")
	[1] "sum(x[i],i==1,8)"
containing the returned value of length(x), and then
send it to the R parser. This returns a list of calls, ie, an expression.
	R> parse(text=paste("sum(x[i],i==1,",length(x),")",sep=""))
	expression(sum(x[i], i == 1, 8))
which is what you need to use as the argument to text().

	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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