Re: R-beta: Including an evaluated function in text expression

Thomas Lumley (thomas@biostat.washington.edu)
Fri, 27 Mar 1998 11:52:06 -0800 (PST)


Date: Fri, 27 Mar 1998 11:52:06 -0800 (PST)
From: Thomas Lumley <thomas@biostat.washington.edu>
To: "Matthew R. Nelson" <mrn@superh.hg.med.umich.edu>
Subject: Re: R-beta: Including an evaluated function in text expression
In-Reply-To: <Pine.3.87.9803271310.A18041-0100000@superh.hg.med.umich.edu>

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@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._