2014-03-26 26 views
0

我无法在网站上的众多相似但不完全相同的问题中找到答案。假设我在变量中有一个字符串,它是我想要使用的plotmath表达式的直接表示,例如, "R^2"。我该如何将它替换成expression,以便它可以绘制为阴谋?我要疯了尝试各种组合,但我不能得到它的工作...将表情字符串替换为表达式

# What I want... 
mn <- "R^2" 
# Using expression directly to show what I'd like to get 
plot(1 , main = expression(R^2)) 

enter image description here

# What I tried to substitute the value of 'mn' into an expression 
plot(1 , main = expression(mn)) 
plot(1 , main = bquote(.(mn))) 
plot(1 , main = bquote(paste(.(mn)))) 
plot(1 , main = do.call(expression , list(mn))) 
plot(1 , main = do.call(expression , list(bquote(.(mn))))) 
plot(1 , main = do.call(expression , list(bquote(paste(.(mn)))))) #getting silly 
plot(1 , main = deparse(substitute(mn , list(mn = mn)))) 

回答

3

这里,parse会做的伎俩:

mn <- "R^2" 

plot(1, main = parse(text = mn)) 

enter image description here

+0

Garrg。我真的想给自己打气。非常感谢你!!!! –

+0

这似乎是一个很好的例子,其中''parse()'真的没有好的选择,'fortune :: fortune(106)'暗示的例外之一。 –