blue sky
2010-Feb-19 17:42 UTC
[R] What is the difference between expression and quote when used with eval()?
I made the following example to see what are the difference between expression and quote. But I don't see any difference when they are used with eval()? Could somebody let me know what the difference is between expression and quote? expr=expression(2*3) quo=quote(2*3) eval(expr) str(expr) class(expr) typeof(expr) mode(expr) attributes(expr) eval(quo) str(quo) class(quo) typeof(quo) mode(quo) attributes(quo)
Peter Dalgaard
2010-Feb-19 18:39 UTC
[R] What is the difference between expression and quote when used with eval()?
blue sky wrote:> I made the following example to see what are the difference between > expression and quote. But I don't see any difference when they are > used with eval()? Could somebody let me know what the difference is > between expression and quote?Expressions are vectors of unevaluated expressions, so one difference is that expressions can have more than one element. Another difference is more subtle: objects of mode "expression" are better at retaining their identity as an unevaluated expression > eval(substitute(2+x,list(x=expression(pi)))) Error in 2 + expression(pi) : non-numeric argument to binary operator > eval(substitute(2+x,list(x=quote(pi)))) [1] 5.141593 The really convincing application of this escapes me for the moment, but the gist of it is that there are cases where a quoted expression may blend in a bit too seemlessly when using computing on the language. Also, expression objects are more easy to recognize programmeatically, quote() may result in objects of mode "call", "name", or one of the base classes. -pd> > expr=expression(2*3) > quo=quote(2*3) > > eval(expr) > str(expr) > class(expr) > typeof(expr) > mode(expr) > attributes(expr) > > eval(quo) > str(quo) > class(quo) > typeof(quo) > mode(quo) > attributes(quo) > > ______________________________________________ > R-help at r-project.org 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.-- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Bert Gunter
2010-Feb-19 19:10 UTC
[R] What is the difference between expression and quote when usedwith eval()?
> as.list(expression( 2*3))[[1]] 2 * 3> as.list(quote( 2*3))[[1]] `*` [[2]] [1] 2 [[3]] [1] 3> identical(as.list(expression( 2*3))[[1]],quote(2*3))[1] TRUE expression() wraps the call into an expression object (as pointed out to me by Gabor Grothendieck). Bert Gunter Genentech Nonclinical Statistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of blue sky Sent: Friday, February 19, 2010 9:43 AM To: r-help at stat.math.ethz.ch Subject: [R] What is the difference between expression and quote when usedwith eval()? I made the following example to see what are the difference between expression and quote. But I don't see any difference when they are used with eval()? Could somebody let me know what the difference is between expression and quote? expr=expression(2*3) quo=quote(2*3) eval(expr) str(expr) class(expr) typeof(expr) mode(expr) attributes(expr) eval(quo) str(quo) class(quo) typeof(quo) mode(quo) attributes(quo) ______________________________________________ R-help at r-project.org 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.