purrr
purrr copied to clipboard
pmap_dfr() does not work well with nested tibbles of one row
When it comes to nested dataframes, pmap_dfr exercises some weird behaviour. I don't know if this is an issue or a desired effect, but worth noting.
tibble(a = 1, b = 2, c = 3) %>%
pmap_dfr(
function(...){
df <- tibble(...)
df
}
)
# Returns
a b c
<dbl> <dbl> <dbl>
1 1 2 3
tibble(a = 1, b = 2, c = 3, d = tibble(a = 1, b = 2, c = 3)) %>%
pmap_dfr(
function(...){
df <- tibble(...)
df
}
)
# returns
a b c d
<dbl> <dbl> <dbl> <dbl>
1 1 2 3 1
2 1 2 3 2
3 1 2 3 3
I believe pmap()
and friends should use vec_slice()
instead of [[
. Moving this to purrr
where pmap()
lives.
library(tidyverse)
ping <- local({
value <- 0L
function() {
value <<- value + 1
value
}
})
tibble(a = 1, b = 2, c = 3, d = tibble(a = 5, b = 6, c = 7)) %>%
pmap_dfr(
function(...){
print(match.call())
data.frame(x = ping(), d = list(...)$d)
}
)
#> .f(a = .l[[1L]][[1L]], b = .l[[2L]][[1L]], c = .l[[3L]][[1L]],
#> d = .l[[4L]][[i]])
#> .f(a = .l[[1L]][[1L]], b = .l[[2L]][[1L]], c = .l[[3L]][[1L]],
#> d = .l[[4L]][[i]])
#> .f(a = .l[[1L]][[1L]], b = .l[[2L]][[1L]], c = .l[[3L]][[1L]],
#> d = .l[[4L]][[i]])
#> x d
#> 1 1 5
#> 2 2 6
#> 3 3 7
Created on 2021-09-17 by the reprex package (v2.0.0)
I'm not sure this one is a bug. It uses the same principles as this, i.e. that this data frame is treated as a list with 3 elements
library(purrr)
library(tibble)
map(tibble(a = 1, b = 2, c = 3), identity)
#> $a
#> [1] 1
#>
#> $b
#> [1] 2
#>
#> $c
#> [1] 3
Unless we had edition 2 of purrr that used vctrs semantics for data frames, I don't think we can change this