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

Romain Francois romain at r-enthusiasts.com
Sun Feb 21 09:34:49 CET 2010


On 02/21/2010 01:45 AM, blue sky wrote:
>
> On Sat, Feb 20, 2010 at 2:40 AM, Romain Francois
> <romain.francois at dbmail.com>  wrote:
>> 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"
>
> Where is the internal inspect documented? Would you please help
> explain what does '@9657560 20', 'g0c2', 'NAM(2)', 'MARK', 'tl' and
> 'gp' stand for?

Reading "R internals" gives some clues, otherwise you can read the 
source, it is only about 200 lines in src/main/inspect.c

-- 
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