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

Romain Francois romain.francois at dbmail.com
Sat Feb 20 09:40:39 CET 2010


On 02/19/2010 10:31 PM, William Dunlap wrote:
>
>> -----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?

There is also the internal inspect function :

 > inspect <- function(x, ...) .Internal(inspect(x,...))

 > inspect( expression(log(1), sqrt(2), trunc(pi)) )
@9657560 20 EXPRSXP g0c2 [NAM(2)] (len=3, tl=153865256)
   @97ab5e8 06 LANGSXP g0c0 []
     @92cf3fc 01 SYMSXP g0c0 [MARK,gp=0x4000] "log"
     @9709a28 14 REALSXP g0c1 [] (len=1, tl=0) 1
   @97aa750 06 LANGSXP g0c0 []
     @92cf204 01 SYMSXP g0c0 [MARK,gp=0x4000] "sqrt"
     @97099e8 14 REALSXP g0c1 [] (len=1, tl=0) 2
   @97aa84c 06 LANGSXP g0c0 []
     @92cf15c 01 SYMSXP g0c0 [MARK,gp=0x4000] "trunc"
     @9347c38 01 SYMSXP g0c0 [MARK,gp=0x4000] "pi"

Romain

> 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


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5



More information about the R-help mailing list