[R] What is the difference between expression and quote whenused with eval()?

William Dunlap wdunlap at tibco.com
Fri Feb 19 22:31:02 CET 2010


> -----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 12:11 PM
> To: Peter Dalgaard
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] What is the difference between expression 
> and quote whenused with eval()?
> 
> On Fri, Feb 19, 2010 at 12:39 PM, Peter Dalgaard
> <p.dalgaard at biostat.ku.dk> wrote:
> > 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.
> 
> I want to see how expression(something) and quote(something) are
> represented in R internally. But it seems that str() doesn't go to
> that low level. Is there a way to show the internal representation?

I use the following, which shows
   `name` class(length)
for each element of a recursive object and
then shows the offspring indented more than
the parent.  It does not go into the attributes,
nor does it try to outwit classes that may
have special methods for as.list(), length(),
or names().  It is handy for checking operator
precedence.

str.language <-
function (object, ..., level=0, name=deparse(substitute(object)))
{
   abbr<-function(string, maxlen=25){
      if(length(string)>1||nchar(string)>maxlen)
         paste(substring(string[1], 1, maxlen), "...", sep="")
      else
         string
   }
   cat(rep("  ", level), sep="")
   if (is.null(name))
      name <- ""
   cat(sprintf("`%s` %s(%d): %s\n", abbr(name),
      class(object), length(object), abbr(deparse(object))))
   if (is.recursive(object)) {
      object <- as.list(object)
      names <- names(object)
      for(i in seq_along(object)) {
         str.language(object[[i]], ...,
            level = level+1, name = names[i])
      }
   }
}

E.g.,

> str.language(function(x,y=log(10))log(x)/y)
`function(x, y = log(10)) ...` function(1): function (x, y = log(10))...
  `x` name(1):
  `y` call(2): log(10)
    `` name(1): log
    `` numeric(1): 10
  `` call(3): log(x)/y
    `` name(1): /
    `` call(2): log(x)
      `` name(1): log
      `` name(1): x
    `` name(1): y
> str.language(expression(log(1), sqrt(2), trunc(pi)))
`expression(log(1), sqrt(2...` expression(3): expression(log(1), sqrt(2...
  `` call(2): log(1)
    `` name(1): log
    `` numeric(1): 1
  `` call(2): sqrt(2)
    `` name(1): sqrt
    `` numeric(1): 2
  `` call(2): trunc(pi)
    `` name(1): trunc
    `` name(1): pi
> str.language(quote(log(pi)))
`quote(log(pi))` call(2): log(pi)
  `` name(1): log
  `` name(1): pi

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 
> 
> 
> >> 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
> >
> 
> ______________________________________________
> 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.
> 



More information about the R-help mailing list