Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DavisVaughan opened this issue Nov 23, 2021 · 0 comments · Fixed by #1242
Closed

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

DavisVaughan opened this issue Nov 23, 2021 · 0 comments · Fixed by #1242
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"

Comments

@DavisVaughan
Copy link
Member

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))

@DavisVaughan DavisVaughan added bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes" labels Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior pivoting ♻️ pivot rectangular data to different "shapes"
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant