jandraor
jandraor

Reputation: 349

Multiple a functions with multiple arguments with purrr

I was able to implement a way to iterate the combination of arguments to a single function:

a <- seq(1, 5, 1)
b <- rep(10, 15, 1)

foo <- function(a, b){ a + b}
foo2 <- function(a, b) {a * b}

result <- invoke_map_dbl(foo, cross2(a, b))

However, I have not been able to iterate those arguments over two functions. I would like to do this:

result <- invoke_map_dbl(list(foo, foo2), cross2(a,b))

Is it possible?

Upvotes: 1

Views: 112

Answers (1)

Andrew Gustar
Andrew Gustar

Reputation: 18425

The problem might be trying to force a list output to dbl. Don't you just need...

invoke_map(list(foo,foo2), a=a, b=b)

[[1]]
[1] 11 12 13 14 15

[[2]]
[1] 10 20 30 40 50

You could unlist to get a single vector, if that is what you want.

Looking at this again, I notice that you are just defining b=10, which looks wrong. So this probably doesn't do what you are asking for, but you will need to be more specific with your question.

Upvotes: 3

Related Questions