My problem is the following; I need to use do.call to execute code similar to this:
do.call(dplyr::select, list(mtcars, mpg))
However this fails with following error message:
Error in do.call(dplyr::select, list(mtcars, mpg)) :
object 'mpg' not found
Quoting mpg works:
do.call(dplyr::select, list(mtcars, "mpg"))
mpg dbl 21 21 22.8 21.4 18.7 18.1
That's great, but now comes the weird part. If I wrap this inside a function, nothing works:
purely <- function(.f){
function(..., .log = "Log start..."){
res <- tryCatch(
do.call(.f, list(...)),
condition = function(cnd) cnd
)
suppressWarnings(
res$result <- if(all(c("message", "call") %in% names(res))){
NA
} else {
res
}
)
list(result = res$result,
log = res$message)
}
}
I wrote purely() to catch errors, warnings and messages. This works on simple functions:
purely(log)(10)
$result
[1] 2.302585
$log
NULL
and conditions get captured:
purely(log)(-10)
$result
[1] NA
$log
[1] "NaNs produced"
So if we go back to our dplyr example:
purely(select)(mtcars, mpg)
$result
[1] NA
$log
[1] "object 'mpg' not found"
Quoting mpg, as before, does not work either:
purely(select)(mtcars, "mpg")
$result
[1] NA
$log
[1] "`scoped_bindings()` is deprecated as of rlang 0.4.2.\nPlease use `local_bindings()` instead."
I'm pretty sure this has something to do with the fact that arguments get passed down a function in the body of purely, but I can't wrap my head around it. Any ideas, help much appreciated.