I am trying to call a function, using a list containing two tibbles as arguments.
I've tried multiple ways (pmap
, do.call
, invoke
, exec
) but none seem to work. It seems that instead of using the two tibbles, the lists of the tibbles are used.
Example data:
list1 <- list(test1 = tibble(a = 1:3, b = 4:6),
test2 = tibble(c = 5:7, d = 7:9))
fun <- function(tbl1,tbl2) tbl1[1,] + tbl2[1,]
What i would like to get is:
print(magic_function_call(fun, list1))
> 6 11
Hardcoded without a function it would be like this:
list1$test1[1,] + list1$test2[1,]
What i get is:
pmap(list1,fun)
> Error in .f(test1 = .l[[1L]][[i]], test2 = .l[[2L]][[i]], ...) :
unused arguments (test1 = .l[[1]][[i]], test2 = .l[[2]][[i]])
do.call(fun,list1)
> Error in (function (tbl1, tbl2) :
unused arguments (test1 = list(1:3, 4:6), test2 = list(5:7, 7:9))
invoke(fun, list1)
> Error in (function (tbl1, tbl2) :
unused arguments (test1 = list(1:3, 4:6), test2 = list(5:7, 7:9))
exec(fun, !!!list1)
> Error in (function (tbl1, tbl2) :
unused arguments (test1 = list(1:3, 4:6), test2 = list(5:7, 7:9))
Doing it by hand works, but feels kinda clunky and as soon as I would want to use a dynamic list with different sizes I would run into problems again.
fun(list1$test1, list1$test2)
> 6 11
Is there any simple way to do this? Am I just missing something?
So to make it short: I want to give two tibbles in a list, as arguments for a function.