typeof ( substitute(expression(),..) ) -- musings

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
29 Jun 1999 20:06:54 +0200


Martin Maechler <maechler@stat.math.ethz.ch> writes:

> [Relates to  math text / normal text mixing in plots ..]
> 
> Peter, I knew you'd know the answer quickly...
> 
> Anyway, one needs to be somewhat careful here [R version  pre 0.64.2]:

..and the first thing to be careful with is the version, expressions
behave slightly different in 0.65!

> 
>     typeof(print( e1 <-            expression(a + b))) # expression 
>     typeof(print(se1 <- substitute(expression(a + b), list(a = 1)))) # language
> 
>     length(e1) # == 1
>     length(se1)# == 2
> 
>     text.e <- function(x,y,e) {
>         text(x,y,paste(deparse(substitute(e)),": "),adj=c(1,0))
> 	text(x,y,e, adj=0)
>     }
>     plot(1,type="n", axes= F)
>     text.e(1,1.4, e1); text.e(1,1.3, se1); text.e(1,1.2, se1[[2]])
> 
> Which shows that you can't use the result of `substitute' directly, but
> rather the 2nd element [[which is *still* not an expression !!]]

Yes. I forgot. se1 is not an expression object but a call to the
expression() - which evaluates to an expression object, i.e. one needs 

se2 <- eval(substitute(expression(a + b), list(a = 1)))

or you can ditch the expression() bit and use

sc2 <- substitute(a + b, list(a = 1))

which is 1 + b, mode "call" and will work with *most* graphics
operations, but there's a caveat: Sometimes language objects get
evaluated when you don't want them to. The notorious example is the
do.call construction in boxplot/bxp.

> This is all +- S-plus (3.4) compatible 
>      {{some details are NOT compatible; e.g. 
> 	     as.expression(se1[[2]])
>        gives `what you expect' in R, but not in S-plus 3.4 or 5.0r3
>      }}
> 
>   e1 is an expression	whereas
>  se1 is a  call (aka "language" in R).
> 
> ----
> Is this documented anywhere? [blue book; language reference; ?? ]

It will be... (It may be in the blue book, but my fading memory of it
says that it is not all that clear on this particular subject.)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._