如何将变量计算为R中轴标签的表达式?

时间:2016-03-04 19:33:12

标签: r eval

目标是允许用户将字符串传递给绘图函数,并将其正确评估为plotmath

问题是如何将评估表达式与其他字符串文本组合在一起。

似乎任何其他字符串或表达式的存在都会使标签的评估无效。

示例:

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

以下是第二个图表的输出,显示缺少label1

enter image description here

预期产出:

enter image description here

其他(可能被误导的)尝试:

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

类似:Evaluate expression given as a string

1 个答案:

答案 0 :(得分:3)

只需使用eval(parse(text=label1)),就像链接的答案所示,或者更简单地使用paste("First part", label1)