library(rlang)
rlang tricks
Decompose Fun and List of Args
<- function(fun = stats::mad, args = list()) {
f
<- rlang::call2(fun, c(1:10, NA), !!!args)
call eval(call)
}
f()
[1] NA
f(args = list(na.rm = TRUE))
[1] 3.7065
<- function(x, fun = "median", pkg = "stats", args = list()) {
g
<- rlang::call2(fun, x, !!!args, .ns = pkg)
call eval(call)
}
g(c(rep(1, 5), rep(2, 3)))
[1] 1
g(c(rep(1, 5), rep(2, 3), NA), "mean", "base", args = list(na.rm = T))
[1] 1.375