This example in "R for Data Science" uses invoke_map which is now retired.
sim <- tribble(
~f, ~params,
"runif", list(min = -1, max = 1),
"rnorm", list(sd = 5),
"rpois", list(lambda = 10)
)
sim %>%
mutate(sim = invoke_map(f, params, n = 10))
If I extract the columns separately then it works with map2 and exec
map2(sim$f, sim$params, function(fn, args) exec(fn, !!!args, n = 10))
However, I cannot get mutate to work with map2 and exec
sim %>%
mutate(sim = map2(f, params, function(fn, args) exec(fn, !!!args, n = 10)))
I get the error "Error: Can't splice an object of type closure because it is not a vector"
Can anyone help with this?
The
!!!operator takes precedence over the creation of the anonymous function. As a result, it evaluatesargsimmediately in the scope of the data frame and, when it finds no such column, in the global scope. One solution is to move the function definition outside themap2call:Another solution is to concatenate the function name and all parameters into a single list, and then pass that list to
execwith its domain lifted: