[R] combining mathematical notation and value substitution

Thomas Lumley tlumley at u.washington.edu
Sat Jun 21 03:03:20 CEST 2003


On Fri, 20 Jun 2003, Faheem Mitha wrote:

>
>
> On Fri, 20 Jun 2003, Uwe Ligges wrote:
>
> >
> > t1 <- theta     # you cannot use theta as variable and math. symbol
> > plot(1:10, main =
> >    substitute("Monotonic Multigamma run (" * n  == len * ", " *
> >      theta == t1 * ").", list(len = len, t1 = t1)))
> >
>
> "Substitute" takes an expression as argument and substitutes the values of
> any variables given in the second argument, correct? However, I am not
> sure what it understands by
>
> "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.

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)
> expression(alpha,beta)[[1]]
alpha

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.


	-thomas




More information about the R-help mailing list