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
See V&R's S PROGRAMMING, esp. section 3.5; and section 6.1 and subsequent of the "R Language Definition." An expression object is the output of parse(), and so is R's representation of a parsed expression. It is a type of list -- a parse tree for the expression. This means that you can actually find the sorts of things you mention by taking it apart as a list:> ex <- parse(text = "x + y") > exexpression(x + y)> class(ex)[1] "expression"> ex[[1]]x + y> ex[[c(1,1)]]`+`> ex[[c(1,2)]]x> ex[[c(1,3)]]y There are few if any circumstances when one should do this: this is the job of the evaluator. There are also special tools available for when you really might want to do this sort of thing -- eg. ?formula, ?terms for altering model specifications. But it is tricky to do right and in full generality -- e.g. ?eval and the above references for some of the issues. Bert Gunter Genentech Nonclinical Statistics South San Francisco, CA 94404 650-467-7374 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Alberto Monteiro Sent: Wednesday, February 28, 2007 1:03 PM To: r-help at stat.math.ethz.ch Subject: [R] What is a expression good for? 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.
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. >