[R] combining mathematical notation and value substitution

Faheem Mitha faheem at email.unc.edu
Sat Jun 21 06:19:05 CEST 2003



> > "Monotonic Multigamma run (" * n  == len * ", " * theta == t1 *
> >
> > Is this a valid expression? My understanding of an expression is that it
> > contains one more more statements.
>
> That's only part  of the expression. This is the full expression
> "Monotonic Multigamma run (" * n  == len * ", " *  theta == t1 * ")."
>
> Now, this looks very strange, but if it looked like
>    a* n==len * b * theta==t1 * d
> it would be a perfectly reasonable  product of five terms,  two of which
> are logical expressions.  I think in fact that it is a call, not an
> expression, but in this case it doesn't matter.
>
> In Uwe's example a, b, and d were strings.  The expression is still
> lexically valid, but it can't be evaluated any more. That's ok, since it
> isn't supposed to be evaluated.

So multiplication is lexically valid in R even between logical
expression and strings?

The * really corresponded to multiplication, then?  Hmm. I see the
choice of * was so that it would not appear in the final math
expression produced by title. Ingenious.

> What you can use in the mathematical annotation functions is parsed but
> unevaluated R code.  You can get this mostly easily as the output of
> quote(), expression() or substitute().
>
>   quote("Parameter " * theta==1)
>   expression("Parameter " * theta==1)
>   substitute("Parameter " * theta==t, list(t=1))
>
> For the purposes of mathematical annotation these are all equivalent,
> though the second returns an `expression' and the other two return a
> `call'.
>
> The second form is occasionally needed, as in
>   legend(locator(1), lty=1:2, legend=expression(alpha,beta))
> which I don't think you can do any other way.
>
> You can think of an expression as a vector of calls, so
> > expression(alpha,beta)[1]
> expression(alpha)

This is an expression (according to ?expression).

> > expression(alpha,beta)[[1]]
> alpha

This is a call.

Can you go into a little more detail here about why alpha here is a
call?  I'm not terribly clear what a call is. If it so similar to an
expression, what distinguishes it from an expression, and why do we
need two similar concepts like this?

Also, would it not be more accurate to describe an expression as a
list of calls? Since, the [[ ]] applied to a list returns a component
of that list, which in this case is apparently a call.

> which is what the manual was trying to say (I think). Much of the time
> you can ignore the difference between `call' and `expression' objects.

                                                        Faheem.




More information about the R-help mailing list