What is the difference between expression and quote when used with eval()?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

What is the difference between expression and quote when used with eval()?

Blue Sky-3
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)

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What is the difference between expression and quote when used with eval()?

Peter Dalgaard
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)
>
> ______________________________________________
> [hidden email] 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
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What is the difference between expression and quote when usedwith eval()?

Bert Gunter
In reply to this post by Blue Sky-3
> 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: [hidden email] [mailto:[hidden email]] On
Behalf Of blue sky
Sent: Friday, February 19, 2010 9:43 AM
To: [hidden email]
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)

______________________________________________
[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What is the difference between expression and quote whenusedwith eval()?

William Dunlap
I think of quote(something) as being equivalent
to expression(something)[[1]].

eval() goes through the outermost expression layer
  > eval(expression(expression(noSuchFunction(noSuchValue))))
  expression(noSuchFunction(noSuchValue))
  > eval(eval(expression(expression(noSuchFunction(noSuchValue)))))
  Error in eval(expr, envir, enclos) :
    could not find function "noSuchFunction"
so I don't think you will ever see a difference between
eval(quote(something)) and eval(expression(something)).
Any low level function other than eval will treat them
differently.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of Bert Gunter
> Sent: Friday, February 19, 2010 11:10 AM
> To: 'blue sky'; [hidden email]
> Subject: Re: [R] What is the difference between expression
> and quote whenusedwith 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: [hidden email]
> [mailto:[hidden email]] On
> Behalf Of blue sky
> Sent: Friday, February 19, 2010 9:43 AM
> To: [hidden email]
> 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)
>
> ______________________________________________
> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What is the difference between expression and quote when used with eval()?

Blue Sky-3
In reply to this post by Peter Dalgaard
On Fri, Feb 19, 2010 at 12:39 PM, Peter Dalgaard
<[hidden email]> 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?


>> 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)
>>
>> ______________________________________________
>> [hidden email] 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
> ~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What is the difference between expression and quote whenused with eval()?

William Dunlap
> -----Original Message-----
> From: [hidden email]
> [mailto:[hidden email]] On Behalf Of blue sky
> Sent: Friday, February 19, 2010 12:11 PM
> To: Peter Dalgaard
> Cc: [hidden email]
> 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
> <[hidden email]> 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)
> >>
> >> ______________________________________________
> >> [hidden email] 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
> > ~~~~~~~~~~ - ([hidden email])              FAX:
> (+45) 35327907
> >
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What is the difference between expression and quote whenused with eval()?

Romain Francois
On 02/19/2010 10:31 PM, William Dunlap wrote:

>
>> -----Original Message-----
>> From: [hidden email]
>> [mailto:[hidden email]] On Behalf Of blue sky
>> Sent: Friday, February 19, 2010 12:11 PM
>> To: Peter Dalgaard
>> Cc: [hidden email]
>> 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
>> <[hidden email]>  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)
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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
>>> ~~~~~~~~~~ - ([hidden email])              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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What is the difference between expression and quote whenused with eval()?

Blue Sky-3
On Sat, Feb 20, 2010 at 2:40 AM, Romain Francois
<[hidden email]> wrote:

> On 02/19/2010 10:31 PM, William Dunlap wrote:
>>
>>> -----Original Message-----
>>> From: [hidden email]
>>> [mailto:[hidden email]] On Behalf Of blue sky
>>> Sent: Friday, February 19, 2010 12:11 PM
>>> To: Peter Dalgaard
>>> Cc: [hidden email]
>>> 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
>>> <[hidden email]>  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?

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: What is the difference between expression and quote whenused with eval()?

Romain François-2
On 02/21/2010 01:45 AM, blue sky wrote:

>
> On Sat, Feb 20, 2010 at 2:40 AM, Romain Francois
> <[hidden email]>  wrote:
>> On 02/19/2010 10:31 PM, William Dunlap wrote:
>>>
>>>> -----Original Message-----
>>>> From: [hidden email]
>>>> [mailto:[hidden email]] On Behalf Of blue sky
>>>> Sent: Friday, February 19, 2010 12:11 PM
>>>> To: Peter Dalgaard
>>>> Cc: [hidden email]
>>>> 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
>>>> <[hidden email]>    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

______________________________________________
[hidden email] 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.