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

blue sky bluesky315 at gmail.com
Sun Feb 21 01:45:29 CET 2010


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?



More information about the R-help mailing list