I have a complex function that returns multiple tibbles (or data frames) as a result of a few computations which are parametrized. These tibbles are shaped differently, so I cannot just return one tibble.
I want to be able to access the different result kinds for each parameter combination, so I create the parameter combinations and map them using pmap_dfr
to get the results. This somewhat works, but this way, in my results, it's impossible to tell which kind of result I am looking at:
library(tidyverse)
foo <- function(.param1, .param2) {
return(tibble(
.param1 = .param1,
.param2 = .param2,
data = list(
ret1 = tibble(ret1_col1 = c(1, 2, 3), ret1_col2 = c(1, 2, 3)),
ret2 = tibble(ret2_col1 = c(1, 2, 3, 4, 5)),
ret3 = tibble(ret3_col1 = c(1, 2), ret3_col2 = c(1, 2), ret3_col3 = c(1, 2))
)
))
}
tibble::tribble(
~.param1, ~.param2,
1, 2,
3, 4
) %>%
pmap_dfr(foo)
#> # A tibble: 6 x 3
#> .param1 .param2 data
#> <dbl> <dbl> <list>
#> 1 1 2 <tibble [3 × 2]>
#> 2 1 2 <tibble [5 × 1]>
#> 3 1 2 <tibble [2 × 3]>
#> 4 3 4 <tibble [3 × 2]>
#> 5 3 4 <tibble [5 × 1]>
#> 6 3 4 <tibble [2 × 3]>
Created on 2019-07-16 by the reprex package (v0.3.0)
For example, for the first row, which <tibble>
is this referring to?
Ideally I'd get the following result:
.param1 .param2 ret1 ret2 ret3
<dbl> <dbl> <list> <list> <list>
1 1 2 <tibble [3 × 2]> <tibble [5 × 1]> <tibble [2 × 3]>
2 3 4 <tibble [3 × 2]> <tibble [5 × 1]> <tibble [2 × 3]>
How can I achieve this?
ret1
,ret2
, etc? – camille