[R] combining mathematical notation and value substitution

Uwe Ligges ligges at statistik.uni-dortmund.de
Sat Jun 21 12:58:26 CEST 2003


Faheem Mitha wrote:
> 
>>>"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.

Yes, thanks.


>>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.

Yes. A very important statement. for mathematical annotation it's just 
parsed in a way. And it's a natural way to use an expression when doing 
*mathematical* annotation.


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

Yes. See Section 6.2 od the R Language Reference.
Consider you introduce a method for a certain new class, that handles 
for any imaginary reason multiplication of logicals and characters. So 
you don't want R to forbid it when constructing an expression or call.


> 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.

 From ?plotmath: "x*y  juxtapose x and y".

BTW: "Ingenious" from those who introduced mathematical annotation in R:
Paul Murrell and Ross Ihaka (2000). An Approach to Providing 
Mathematical Annotation in Plots, Journal of Computational and Graphical 
Statistics, 9(3): 582–599.


>>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.

In the reference given in my former mail their is an example how to 
construct such an expression from calls (constructed by substitute()) 
for legend() with do.call().


>>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?  

Thomas already told it and ?expression says as well: "expression returns 
a vector of mode "expression" containing its arguments as unevaluated 
``calls''."


 > 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?

Obviously a question for Bill Venables. I admit I've not thought about 
it before. In this context the article
  W. N. Venables (2002). Programmer’s Niche, R News, 2(2): 24–26,
  ISSN 1609-3631, http://CRAN. R-project.org/doc/Rnews/,
and
  Venables & Ripley (2000): S Programming, Springer.
are nice references.

Uwe Ligges

> 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