Search code examples
rdplyrtidyversepurrrrlang

Using mutate with map2 and exec instead of invoke_map


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?


Solution

  • The !!! operator takes precedence over the creation of the anonymous function. As a result, it evaluates args immediately 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 the map2 call:

    myfun <- function(fn, args) exec(fn, !!!args, n = 10)
    
    sim %>% 
      mutate(sim = map2(f, params, myfun))        # Now works
    

    Another solution is to concatenate the function name and all parameters into a single list, and then pass that list to exec with its domain lifted:

    sim %>%
        mutate(temp = map2(f, params, c, n=10),
               sim  = map(temp, lift(exec)),
               temp = NULL)