Dear People,
I need to make a label which both contains math notation as well as
substitutes a value for an object. In the following label, len and theta
are one dim variables, and I am substituting their values appropriately.
This label looks fine except that I want the greek symbol for theta to
appear instead of the word `theta'. How can I do so most easily? I don't
understand the underlying mechanisms well enough to work it out for
myself. Thanks in advance.
Please cc me. I'm not subscribed to the list.
Faheem.
main=paste("Monotonic Multigamma run (n=",
deparse(substitute(len)),", ",
expression(theta),"=", deparse(substitute(theta)),").")
combining mathematical notation and value substitution
10 messages · Faheem Mitha, Thomas Lumley, Uwe Ligges
On Fri, 20 Jun 2003, Faheem Mitha wrote:
Dear People, I need to make a label which both contains math notation as well as substitutes a value for an object. In the following label, len and theta are one dim variables, and I am substituting their values appropriately. This label looks fine except that I want the greek symbol for theta to appear instead of the word `theta'. How can I do so most easily?
[snip]
main=paste("Monotonic Multigamma run (n=",
deparse(substitute(len)),", ",
expression(theta),"=", deparse(substitute(theta)),").")
Hmm. Tried this, didn't work either. Inspired by pg 32 of "R for
Beginners".
main=paste("Monotonic Multigamma run",
as.expression(substitute(n==length,list(length=len))),
as.expression(substitute(theta==th,list(th=theta))))
Faheem.
On Fri, 20 Jun 2003, Faheem Mitha wrote:
Hmm. Tried this, didn't work either. Inspired by pg 32 of "R for
Beginners".
main=paste("Monotonic Multigamma run",
as.expression(substitute(n==length,list(length=len))),
as.expression(substitute(theta==th,list(th=theta))))
Sorry, stupid of me. I guess that paste converts things back to character
strings, so that won't work.
Faheem.
Faheem Mitha wrote:
Dear People,
I need to make a label which both contains math notation as well as
substitutes a value for an object. In the following label, len and theta
are one dim variables, and I am substituting their values appropriately.
This label looks fine except that I want the greek symbol for theta to
appear instead of the word `theta'. How can I do so most easily? I don't
understand the underlying mechanisms well enough to work it out for
myself. Thanks in advance.
Please cc me. I'm not subscribed to the list.
Faheem.
main=paste("Monotonic Multigamma run (n=",
deparse(substitute(len)),", ",
expression(theta),"=", deparse(substitute(theta)),").")
No, it won't work that way, because you have to specify an S expression
in order to get mathematical annotation. An expression within paste()
will be converted to a character string.
What you ca do is the other way round:
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)))
See also ?plotmath or that small article in R News:
Ligges (2002): R Help Desk: Automation of Mathematical Annotation in
Plots. R News 2(3), 32-34.
Uwe Ligges
On Fri, 20 Jun 2003, Uwe Ligges wrote:
Faheem Mitha wrote:
[snip]
main=paste("Monotonic Multigamma run (n=",
deparse(substitute(len)),", ",
expression(theta),"=", deparse(substitute(theta)),").")
No, it won't work that way, because you have to specify an S expression
in order to get mathematical annotation. An expression within paste()
will be converted to a character string.
What you ca do is the other way round:
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)))
See also ?plotmath or that small article in R News:
Ligges (2002): R Help Desk: Automation of Mathematical Annotation in
Plots. R News 2(3), 32-34.
Thank you for your help. Your expression works correctly. I've been trying
to understand why. I read your article, and I had already looked
previously at ?plotmath. However, the syntax of the above is still not
clear to me.
"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. The R-lang manual says, not entirely
intelligibly
********************************************************************* "In
R one can have objects of type `"expression"'. An _expression_ contains
one or more statements. A statement is a syntactically correct collection
of tokens. Expression objects are special language objects which they
contain parsed, but unevaluated R statements. The main difference is that
an expression object can contain several such expressions. Another more
subtle difference is that objects of type `"expression"' are only
evaluated when explicitly passed to `eval', whereas other language objects
may get evaluated in some unexpected cases.
An expression object behaves much like a list and its components should be
accessed in the same way as the components of a list."
*********************************************************************
Firstly, I'm having difficulty parsing the sentence "Expression objects
are special language objects which they contain parsed, but unevaluated R
statements." Should the word "which" be removed? Then, the next sentence
says "The main difference is that an expression object can contain several
such expressions." The main difference from what? Lastly, if "An
expression object behaves much like a list" what are the individual
components? Are they expressions, and if so, how are they separated from
each other? I apologise for my cluelessness.
In any case, an expression object contains several statements, or several
expressions (not quite clear on the distinction). So, what are the
statements here?
You have character strings adjoined by eg. `n == len' which I agree is a
sensible statement. I'm guessing that R thinks of each character string as
a separate statement. If I do "foo" on the R command line it echoes it
back to me, but "foo" "bar" gives an error.
Can you explain what the `*' are doing here? Perhaps separating
statements? I've looked for use of this in a similar context but was
unable to find anything. Thanks again.
BTW, does "Introductory Statistics with R" by Peter Dalgaard, contain
disscussion of language issues, including "computing on the language"
stuff?
Faheem.
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
"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.
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.
On Sat, 21 Jun 2003, Uwe Ligges wrote:
Faheem Mitha wrote:
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''."
Yes, but it is just a call by definition then? If you put anything in the place of alpha it would be 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?
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.
I've got "S Programming" and spent some of yesterday reading it. It
doesn't go into detail into the difference between expressions and calls.
It does have a table on pg 66 which gives examples of different modes.
Faheem.
1 day later
On Sat, 21 Jun 2003, Faheem Mitha wrote:
So multiplication is lexically valid in R even between logical expression and strings?
yes, in the sense that the lexer and parser won't choke when trying to read the statement. You could define a method for "*" that multiplied character strings (you might have to work through defining Ops.character)
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.
No, it would be less accurate (though arguably clearer). An expression is not actually internally represented as a list of calls, just as list(quote(x*y), quote(a+b)) is represented as a list of calls, not an expression. Now, an expression could have been just a list of calls (it works in LISP), but in fact it isn't. The problem in writing the R language definition is in trying to be both precise and helpful (the paragraph you quoted is arguably neither, but that's why it's a `draft'). The precise difference between an expression and a call is that an expression is of mode "expression" and a call is of mode "call" (and a list of calls is of mode "list"). This isn't very helpful, which is why you get the handwaving about an expression being like a list of calls. -thomas