How can I get a do.call
with a variable list of arguments and functions to work with the standard evaluation version of summarise_
in dplyr?
## Some sample data, function, and variables to interpolate
set.seed(0)
dat <- data.frame(a=runif(10), b=runif(10))
fn <- function(x, y) IQR(x / y, na.rm = TRUE)
funs <- list(fn="fn")
targs <- list("a", "b")
This is the lazyeval::interp
I'm trying to make work
library(dplyr)
interp(~do.call(fn, xs), .values=list(fn=funs$fn, xs=targs))
# ~do.call("fn", list("a", "b"))
but it doesnt work,
dat %>%
summarise_(out = interp(~do.call(fn, xs), .values=list(fn=funs$fn, xs=targs)))
Expected result
dat %>%
summarise(out = do.call(fn, list(a, b)))
# out
# 1 1.084402
If I add in some print statements, I know the problem is that the "a" and "b" aren't being interpreted properly, but I haven't been able to figure out how to quote them properly.
fn <- function(x, y) { print(x); print(y); IQR(x / y, na.rm = TRUE) }
dat %>%
summarise_(out = interp(~do.call(fn, xs), fn=funs$fn, xs=targs))
# [1] "a"
# [1] "b"
# Error: non-numeric argument to binary operator