Skip to content

Improve on error message when values_fn doesn't result in 1 value per row #1238

@DavisVaughan

Description

@DavisVaughan
library(tidyr)

df <- tibble(
  name = c("a", "a"),
  value = c(1, 2)
)

df
#> # A tibble: 2 × 2
#>   name  value
#>   <chr> <dbl>
#> 1 a         1
#> 2 a         2

df %>% 
  pivot_wider(
    names_from = name,
    values_from = value,
    values_fn = list(value = ~.x)
  )
#> Error in `$<-.data.frame`(`*tmp*`, "val", value = c(1, 2)): replacement has 2 rows, data has 1

It should be reasonably fast to call list_sizes() here and then check that they are all size 1

out$val <- vec_c(!!!map(out$val, values_fn))

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorpivoting ♻️pivot rectangular data to different "shapes"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions