问题 当argument是一个语言对象时,通过do.call添加ggtitle


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

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

直接调用该函数可以按预期工作。
但是,通过调用函数 do.call(f, ..) 抛出错误时 TITLE 是一个 language 目的

## 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() 参数是语言对象时是否正确使用?


11159
2018-05-07 17:15


起源



答案:


使用

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

代替。问题是运行do.call时正在评估表达式。通过设置 quote=TRUE 它将引用参数,以便在将它们传递给时将它们取消评估 f。你也可以明确引用 TIT

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

11
2018-05-07 17:20



那是你想的。我不能直接引用这个论点,因为我正在以编程方式收集其中的几个。但是使用了 quote param工作 - Ricardo Saporta


答案:


使用

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

代替。问题是运行do.call时正在评估表达式。通过设置 quote=TRUE 它将引用参数,以便在将它们传递给时将它们取消评估 f。你也可以明确引用 TIT

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

11
2018-05-07 17:20



那是你想的。我不能直接引用这个论点,因为我正在以编程方式收集其中的几个。但是使用了 quote param工作 - Ricardo Saporta