Ric*_*rta 11 r ggplot2 do.call
f <- function(PLOT, TITLE) {
PLOT + ggtitle(TITLE)
}
Run Code Online (Sandbox Code Playgroud)
直接调用该函数可以按预期工作.
但是,作为对象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"
Run Code Online (Sandbox Code Playgroud)
这当然只有在TIT语言对象时才会发生
TIT.char <- "This is some text\nHere is some more text"
do.call(f, list(P, TIT.char))
## No Error
Run Code Online (Sandbox Code Playgroud)
do.call()当参数是语言对象时如何正确使用?
MrF*_*ick 11
使用
do.call(f, list(P, TIT), quote=TRUE)
Run Code Online (Sandbox Code Playgroud)
代替.问题是运行do.call时正在评估表达式.通过设置quote=TRUE它将引用参数,以便在传递它们时将它们取消评估f.你也可以明确引用TIT
do.call(f, list(P, quote(TIT)))
Run Code Online (Sandbox Code Playgroud)