# Re: [Rd] substitute and expression

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Tue, 17 Jul 2007 00:24:05 +0200

Deepayan Sarkar wrote:
> Hi,
>
> I'm trying to understand whether the use of substitute() is
> appropriate/documented for plotmath annotation. The following two
> calls give the same results:
>
>
>> plot(1:10, main = expression(alpha == 1))
>> do.call(plot, list(1:10, main = expression(alpha == 1)))
>>
>
> But not these two:
>
>
>> plot(1:10, main = substitute(alpha == a, list(a = 2)))
>> do.call(plot, list(1:10, main = substitute(alpha == a, list(a = 2))))
>>
>
> (as a consequence, xyplot(..., main = substitute(alpha)) doesn't
> currently work.)
>
> On the other hand, this works:
>
>
>> foo <- function(x) plot(1, main = x)
>> foo(substitute(alpha))
>>
>
> I'm not sure how to interpret ?plotmath; it says
>
> If the 'text' argument to one of the text-drawing functions
> ('text', 'mtext', 'axis', 'legend') in R is an expression, the
> argument is interpreted as a mathematical expression...
>
> and uses substitute() in its examples, but
>
>

>> is.expression(substitute(alpha == a, list(a = 1)))
>>
> [1] FALSE
>
I think you need to take plotmath out of the equation and study the difference between objects of mode "call" and those of mode "expression". Consider this:

> f <- function(...)match.call()
> do.call(f, list(1:10, main = substitute(alpha == a, list(a = 2)))) function(...)match.call()
(1:10, main = alpha == 2)
> do.call(list, list(1:10, main = substitute(alpha == a, list(a = 2))))
Error in do.call(list, list(1:10, main = substitute(alpha == a, list(a = 2)))) :