[R] What is a expression good for?

Gabor Grothendieck ggrothendieck at gmail.com
Thu Mar 1 01:06:51 CET 2007


You can evaluate it, differentiate it, pick apart its components,
use it as a title or legend in a plot, use it as a function body
and probably other things too:

e <- expression(x+y)

eval(e, list(x = 1, y = 2)) # 3

D(e, "x")

e[[1]][[1]] # +
e[[1]][[2]] # x
e[[1]][[3]] # y

plot(1:10, main = e)
legend("topleft", e, pch = 1)


f <- function(x,y) {}
body(f) <- e
f(1,2) # 3

On 2/28/07, Alberto Monteiro <albmont at centroin.com.br> wrote:
> I mean, I can generate a expression, for example, with:
>
> z <- expression(x+y)
>
> But then how can I _use_ it? Is it possible to retrieve
> information from it, for example, that z is a sum, its
> first argument is x (or expression(x)) and its second
> argument is y?
>
> Alberto Monteiro
>
> ______________________________________________
> R-help at 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
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list