1

Objective is to allow user to pass a string to a plotting function and have it evaluated correctly as plotmath.

Question is how to combine an evaluated expression with other string text.

It seems the presence of any other string or expression nullifies the evaluation of the label.

Example:

label1 <- 'degree~C'

plot(1:10, type='l', xlab=bquote(.(parse(text=label1))))  #evaluated correctly
plot(1:10, type='l', xlab=bquote('Some text'~~Omega^2~~.(parse(text=label1))))  #missing degree C

Here is the output of the second plot showing that label1 is missing:

enter image description here

Expected output:

enter image description here

Other (possibly misguided) attempts:

plot(1:10, type='l', xlab=substitute(paste('Some text'~~Omega^2~~mystring), list(mystring=label1))) #string not evaluated
plot(1:10, type='l', xlab=substitute(paste('Some text'~~Omega^2~~mystring), list(mystring=parse(text=label1)))) #string missing entirely
plot(1:10, type='l', xlab=substitute(paste('Some text'~~Omega^2~~mystring), list(mystring=expression(text=label1)))) #string missing entirely

Similar: Evaluate expression given as a string

Community
  • 1
  • 1
Minnow
  • 1,733
  • 2
  • 26
  • 52
  • 1
    `eval(parse(text=label1))`. Parse just creates an expression. Use `class` on the objects to find out... That's what the answer in the question you linked also clearly states. – Benjamin Mar 04 '16 at 19:58
  • Egads, you are indeed right. Thank you! – Minnow Mar 04 '16 at 20:43

1 Answers1

4

Just use eval(parse(text=label1)), like the linked answer suggests, or more simply, paste("First part", label1).

Benjamin
  • 11,560
  • 13
  • 70
  • 119