当参数是语言对象时通过 do.call 添加 ggtitle

adding ggtitle via do.call when argument is a language object

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

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

直接调用函数按预期工作。
但是,当 TITLElanguage 对象

时,通过 do.call(f, ..) 调用函数会引发错误
## 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()

使用

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

相反。问题是当你 运行 do.call 时,你的表达式正在被评估。通过设置 quote=TRUE,它会引用参数以在将它们传递给 f 时不对它们求值。您也可以显式引用 TIT

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