Re: [R] Multiple expressions, when using substitute()

From: Marc Schwartz <MSchwartz_at_mn.rr.com>
Date: Sun 02 Oct 2005 - 01:10:23 EST

On Sat, 2005-10-01 at 20:32 +1000, John Maindonald wrote:
> expression() accepts multiple expressions as arguments, thus:
>
> plot(1:2, 1:2)
> legend("topleft",
> expression(y == a * x^b,
> "where "* paste(y=="wood; ",
> x=="dbh")))
>
> Is there a way to do this when values are to be substituted
> for a and b? i.e., the first element of the legend argument
> to legend() becomes, effectively:
> substitute(y == a * x^b, list(a = B[1], b=B[2]))

John,

Try this:

a <- 5
b <- 3

L <- list(bquote(y == .(a) * x^.(b)),

          "where y = wood; x = dbh")

plot(1:2, 1:2)

legend(legend = do.call("expression", L),

       "topleft")

Note the creation of the list 'L', which uses bquote() and then the .(Var) construct, where 'Var' are your variables to be replaced. Then in the legend() call, the use of do.call() to apply expression() to the elements of list 'L'.

HTH, Marc Schwartz



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sun Oct 02 01:14:36 2005

This archive was generated by hypermail 2.1.8 : Sun 23 Oct 2005 - 18:07:56 EST