问题
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:
Expected output:
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
回答1:
Just use eval(parse(text=label1))
, like the linked answer suggests, or more simply, paste("First part", label1)
.
来源:https://stackoverflow.com/questions/35804590/how-to-evaluate-a-variable-as-an-expression-for-axis-label-in-r