Faheem Mitha
2003-Jun-20 06:47 UTC
[R] combining mathematical notation and value substitution
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)),").")
Faheem Mitha
2003-Jun-20 07:10 UTC
[R] Re: combining mathematical notation and value substitution
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.
Uwe Ligges
2003-Jun-20 07:25 UTC
[R] combining mathematical notation and value substitution
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
Thomas Lumley
2003-Jun-23 04:28 UTC
[R] combining mathematical notation and value substitution
On Sun, 22 Jun 2003, Faheem Mitha wrote:> If I'm doing this correctly, R does not seem to think it is a call. > > > is.call("Monotonic Multigamma run (" * n == len * ", " * theta == t1 > * ").") > Error in "Monotonic Multigamma run (" * n : > non-numeric argument to binary operatorR is trying to *evaluate* "Monotonic Multigamma run ("* n==len etc which doesn't work. Remember, is.call(), like any normal function, will be passed the *value* of its arguments. You could try is.call(quote("Monotonic Multigamma run("*n==len)) which is TRUE.> It considers it a valid R expression though. > > > (mode(expression("Monotonic Multigamma run (" * n == len * ", " * theta > == t1 * ")."))) > [1] "expression" >That's because expression() returns an expression.> > The clearest description I have seen of a call is in S Poetry, where it > says > > "Mode call represents objects that are calls to a function. The first > component of a call is the name (mode name) of the function being called. > The rest of the call is the arguments given." > > This certainly is how calls are constructed using call(...), but I'm not > sure how it fits in with an expression like the one above. What is the > function being called in that case, for example?Well, we can find out. It must be either * or ==, but it isn't immediately obvious which one ends up at the top level> thing <- quote("Monotonic Multigamma Run ("*n==len* ", " * theta==t1*").")> mode(thing)[1] "call"> length(thing)[1] 3> thing[[1]]=> thing[[2]] "Monotonic Multigamma Run (" * n == len * ", " * theta> thing[[3]]t1 * ")."> mode(thing[[2]])[1] "call"> mode(thing[[3]])[1] "call"> thing[[2]][[1]]=> thing[[3]][[1]] * So it is a call to ==, with two arguments, each itself a call. The first arguemetn is also a call to == and the second is a call to *. And so on in a tree structure.> Maybe I'd understand this stuff better if I knew Lisp. I'm very ignorant > about programming language paradigms. I've looked at S Poetry, and S > Programming. While they were helpful, I don't feel I've got a clear > picture. Further recommended reading? >I don't know about further reading. I think this is probably easier to learn when you have a specific problem to solve. I learned mostly by porting the survival package. -thomas