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

1

11

Consider a simple function, which adds a ggtitle to a grob

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

Calling the function directly works as expected.
However, calling the function via do.call(f, ..) throws an error when TITLE is a language object

## 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"

This of course only happens when TIT is a language object

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

How can do.call() be used correctly when arguments are language objects?

Poult answered 7/5, 2015 at 17:15 Comment(0)
H
11

Use

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

instead. The problem is that your expression is being evaluated when you run do.call. By setting quote=TRUE it will quote the arguments to leave them un-evaluated when passing them along to f. You can also explicitly quote TIT

do.call(f, list(P, quote(TIT)))
Helwig answered 7/5, 2015 at 17:20 Comment(1)
That did it think you. I can't quote the argument directly, as I am collecting several of them programatically. But using the quote param worksPoult

© 2022 - 2024 — McMap. All rights reserved.