6

From sfsmisc package I have an expression and I want to add a text before that. How can I add text on an expression?

library(sfsmisc)
v <- pretty10exp(500)
title <- paste("some text ", v)
plot(1:5, 1:5, main = title)

This plots title as some text 5 %*% 10^2 but not the formatted text.

cNinja
  • 145
  • 6
  • @YannisVassiliadis: cNinja means as would be printed if it were interpreted as a plotmath expression. It's a mechanism a bit similar to TeX symbol parsing. See `?plotmath` – IRTFM May 03 '18 at 00:14

2 Answers2

6

I think if you use parse it will satisfy the R interpreter. parse returns an unevaluated 'expression'-classed value. You just need to make sure there are tildes (~) where you want spacing:

v <- pretty10exp(500)
title <- parse(text= paste("some ~text ~", v ) ) 
plot(1:5, 1:5, main = title)

title
#expression(some ~text ~ 5 %*% 10^2)

Expressions in R need to satisfy the parsing rules of the R language, e but the symbols or tokens don't need to refer to anything in particular in the applications since they will only be displayed "as is". So I decided to use parse as the constructor of the expression rather than trying to prepend text onto an existing expression. Between every token, there needs to be a separator. There can also be function-type use of parentheses "(" or square brackets "[", but they will need to be properly paired.

> expression( this won't work)   # because of the lack of separators
Error: unexpected symbol in "expression( this won"
> expression( this ~ won't *work)
+                           # because it fails to close after the single quote
> expression( this ~ won\'t *work)
Error: unexpected input in "expression( this ~ won\"
> expression( this ~ won\\'t *work)
Error: unexpected input in "expression( this ~ won\"
> expression( this ~ will *work)
expression(this ~ will * work)      # my first successful expression
> expression( this ~ will *(work)
+ but only if properly closed)     # parsing continued to 2nd line after parens.
Error: unexpected symbol in:
"expression( this ~ will *(work)
but"
> expression( this ~ will *(work)    # no error so far anyway
+ *but~only~if~properly~closed)
Error: unexpected '~' in:
"expression( this ~ will *(work)
*but~only~if~"
> expression( this ~ will *(work)
+ *but~only~'if'~properly~closed)
# At last ... acceptance
expression(this ~ will * (work) * but ~ only ~ "if" ~ properly ~ 
    closed)

That last one comes about because there are a few (very few) reserved words in R and if happens to be one of them. See ?Reserved

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • +1 congrats you did it, I got stuck at `all.equal(noquote(paste("expression(", (paste('"some text"', v, sep = " ~ ")), ")")), expression("some text" ~ 5 %*% 10^2))`. I'd appreciate if you'd have a minute to add explanation to your answer. – jay.sf May 02 '18 at 23:18
  • I'm ashamed to admit it took me about 20 false starts. Nonetheless your congrats and upvote have just pushed me over the 200 rep/per day landmark and I'm that little bit closer to my "Legendary" badge, so I thank you. There're probably functions in the `rlang`-package that might also succeed. – IRTFM May 02 '18 at 23:45
  • Welcome! You also should earn a "rockbiter" batch, this was really a hard one! cheers – jay.sf May 03 '18 at 00:03
  • It's kind of weird what level of disconnect there is between the amount of time spent on answering a question and the number of votes offered. This one was pretty quick in the grand scheme of things. I worked all morning on this other one (and got 0): https://stackoverflow.com/questions/49959319/string-wrapping-text-data-in-risk-table-in-surv-miner-for-ggplot2 – IRTFM May 03 '18 at 00:10
1

I'm not sure if it's possible to do this automatically, but if you don't rely on this, you could do

plot(1:10, 1:10, main = expression("some text" ~ 5 %*% 10^2))

yields:

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110