【问题标题】:Is paste a special argument in plotmath expressions?在 plotmath 表达式中粘贴一个特殊的参数吗?
【发布时间】:2013-11-02 23:45:22
【问题描述】:

当为情节标题提供plotmath 表达式时,我认为paste 的工作方式与paste 一样,但现在我不确定。它是plotmath 函数的特殊参数吗?在 plotmath 中,paste() 的行为类似于 paste0()paste0 被直接引用,而 paste()sep 参数被忽略,但未在表达式中引用。 paste() 是如何解释的?请参阅下面的四个示例:

#  Data to plot
x <- seq( 55 , 70 , length = 2001 )
probs <- dexp( x , rate = 5 / 60  )
s <- sample( x , 10000 , prob = probs , repl = TRUE )


#  Some plots
par(mfrow = c(2,2) )
#1)  paste() behaves like paste0() here
hist( s , breaks = 30 , 
    main = expression( paste( "1: Sampling of" , Phi , "Distribution" ) ) ,
    xlab = "No spaces in title using paste()" , axes = F )

#2)  add spaces in the expression where they are needed
hist( s , breaks = 30 , 
    main = expression( paste( "2: Sampling of "  , Phi , " Distribution" ) ) ,
    xlab = "Single spaces in titles: paste() behaves like paste0()" , axes = F )

#3)  Don't put spaces, use a sep argument - ignored
hist( s , breaks = 30 , 
    main = expression( paste( "3: Sampling of"  , Phi , "Distribution" , sep = " " ) ) ,
    xlab = "No spaces using paste(), `sep` argument is ignored but not quoted in expression" , axes = F )

#4)  paste0 is not recognised
hist( s , breaks = 30 , 
    main = expression( paste0( "4: Sampling of " , Phi , " Distribution" ) ) ,
    xlab = "paste0() not recognised" , axes = F )

【问题讨论】:

  • 你可能需要eval(expression(paste...)) --- 至少我在控制台级别是这样做的以返回一个字符字符串。
  • @CarlWitthoft 说我的情节 1 的示例代码?我发现 R 会搜索 Phi 对象(Phi 是一个绘图功能,应该被解释为符号)。
  • 哼。我似乎记得一个关于如何做你想做的事的教程,但不知道它在哪里。
  • @CarlWitthoft:您的评论暴露了对绘图表达式的根本误解。如果您想使用text 将字符串打印到绘图设备,只需将其括在引号中即可。如果你想用显示的空格连接表达式或字符串,请使用~。在 ?plotmath 页面中使用 paste 和插入的 " " 的示例并不真正需要该功能,可以更紧凑地写为:text(8, 3, expression(frac(1, sigma*sqrt(2*pi))~~e^{ frac( -(x-mu)^2, 2*sigma^2)}), cex = 1.2)
  • @DWin,好吧,我确实说我完全忘记了——其中包括?plotmath 页面:-)。但是感谢您在评论之前提醒我 rtfm;这是我应得的。

标签: r graphics syntax plot plotmath


【解决方案1】:

的确,plotmath 中的paste 不是经典的paste。见?plotmath:

paste(x, y, z) 并列 x、y 和 z

pasteplotmath 的上下文中没有sep 参数。

并且在源代码plotmath.c中可以看到paste被重新定义了:

/*----------------------------------------------------------------------
*
* Code for Concatenate Expressions
*
*/

static int ConcatenateAtom(SEXP expr)
{
    return NameAtom(expr) && NameMatch(expr, "paste");
}

static BBOX RenderConcatenate(SEXP expr, int draw, mathContext *mc,
                         pGEcontext gc, pGEDevDesc dd)
{
    BBOX bbox = NullBBox();
    int i, n;

    expr = CDR(expr);
    n = length(expr);

    for (i = 0; i < n; i++) {
        bbox = CombineBBoxes(bbox, RenderElement(CAR(expr), draw, mc, gc, dd));
        if (i != n - 1)
         bbox = RenderItalicCorr(bbox, draw, mc, gc, dd);
        expr = CDR(expr);
    }
    return bbox;
}

稍后在文件中分派:

static BBOX RenderFormula(SEXP expr, int draw, mathContext *mc,
                         pGEcontext gc, pGEDevDesc dd)
{
    SEXP head = CAR(expr);

    ....
    else if (ConcatenateAtom(head))
        return RenderConcatenate(expr, draw, mc, gc, dd);
    ....

(话虽这么说我对C一无所知,所以我可能错了)

【讨论】:

  • +1 非常有趣,谢谢!我认为这回答了我的问题。
猜你喜欢
  • 2022-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多