2

I know how to get my result if I use one long expression:

expression(paste(M[O[2]], " (mg ", O[2], " ", h^-1, " ", kg^-1, ")"))

But I tried to break this in chunks, so that the user could select a time and a mass unit (to add flexibility), and then put together the complete expression. Say I just reuse the time and mass units from above but into 3 chunks, which I then try to join again:

a = expression(paste(M[O[2]], " (mg ", O[2]))  
b = expression(kg^-1)  
c = expression(h^-1)  

I have tried

text(x, y, expression(a b c))  
text(x, y, paste(a, b, c))  
text(x, y, expression(paste(a, b, c)))  

but this results in a, b and c remaining characters a, b and c, not the expressions created above. I have tried playing with bquote but I admit to not mastering it and I could not get it to concatenate the 3 plotmath expressions either.

Any suggestion?

Thanks,

Denis

Denis Chabot
  • 71
  • 1
  • 4
  • I am sorry about the formatting in my post, things that were on separate lines got put onto a single line and I don't see how to edit a post. – Denis Chabot Dec 10 '15 at 03:40

1 Answers1

4

Create a list of a, b and c and apply [[1]] to each component to get rid of the expression part and then substitute the results into a * b * c * ")" like this:

e <- substitute(a * b * c * ")", lapply(list(a = a, b = b, c = c), "[[", 1))
plot(0, main = e)

(continued after plot)

screenshot

Note that it would be easier if a, b and c were defined using quote rather than expression. In that case it simplifies to:

a <- quote(paste(M[O[2]], " (mg ", O[2]))  
b <- quote(kg^-1)  
c <- quote(h^-1)  
e <- substitute(a * b * c * ")", list(a = a, b = b, c = c))
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Fantastic, thank you very much. I like the quote version better, of course, I'll read about quote, which I never used before. Thanks a lot, Denis – Denis Chabot Dec 11 '15 at 17:28