如何将变量作为R中轴标签的表达式求值? [英] How to evaluate a variable as an expression for axis label in R?

查看:75
本文介绍了如何将变量作为R中轴标签的表达式求值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是允许用户将字符串传递给绘图函数,并使其正确评估为问题是如何将求值表达式与其他字符串文本组合在一起.

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

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

示例:

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:

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

预期输出:

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

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

类似:评估以字符串形式给出的表达式

推荐答案

只需使用eval(parse(text=label1)),就像链接的答案所建议的那样,或更简单地说,是paste("First part", label1).

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

这篇关于如何将变量作为R中轴标签的表达式求值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆