[R] What is a expression good for?

Bert Gunter gunter.berton at gene.com
Wed Feb 28 22:54:39 CET 2007


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")
> ex
expression(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.



More information about the R-help mailing list