【问题标题】:Substitute plotmath character string into expression将 plotmath 字符串替换为表达式
【发布时间】:2014-05-05 01:16:38
【问题描述】:

我在网站上的众多类似但不完全相同的问题中找不到答案。假设我在变量中有一个字符串,它是我要使用的绘图表达式的直接表示,例如"R^2"。如何将其替换为 expression 以便将其呈现为 plotmath?我会疯狂尝试每一种组合,但我无法让它发挥作用......

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

#  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 ) ) ) )

【问题讨论】:

    标签: r graphics plot expression plotmath


    【解决方案1】:

    在这里,parse 可以解决问题:

    mn <- "R^2"
    
    plot(1, main = parse(text = mn))
    

    【讨论】:

    • 这似乎是一个很好的例子,说明确实没有比 parse() 更好的替代品,fortunes::fortune(106) 暗示的例外之一。
    猜你喜欢
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2017-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多