11

考虑一个简单的函数,它将 ggtitle 添加到 grob

f <- function(PLOT, TITLE) {
  PLOT + ggtitle(TITLE)
}

直接调用函数按预期工作。但是,当是一个对象时
,调用该函数do.call(f, ..)会引发错误TITLElanguage

## Sample Data
TIT <- bquote(atop("This is some text",  atop(italic("Here is some more text"))))
P   <- qplot(x=1:10, y=1:10, geom="point")

## WORKS FINE
f(P, TIT)

## FAILS
do.call(f, list(P, TIT))
## Error in labs(title = label) : could not find function "atop"

这当然只发生在TIT语言对象

TIT.char <- "This is some text\nHere is some more text"
do.call(f, list(P, TIT.char))
## No Error

当参数是语言对象时如何do.call()正确使用?

4

1 回答 1

11

利用

do.call(f, list(P, TIT), quote=TRUE)

反而。问题是当您运行 do.call 时正在评估您的表达式。通过设置quote=TRUE,它将引用参数以在将它们传递给时不对其进行评估f。您也可以明确引用TIT

do.call(f, list(P, quote(TIT)))
于 2015-05-07T17:20:03.853 回答