Skip to content

Commit

Permalink
Use .by syntax in pivot_wider dplyr helper message (#1529)
Browse files Browse the repository at this point in the history
Fixes #1516
  • Loading branch information
boshek authored Nov 4, 2023
1 parent b4d1ec2 commit 4afb8ca
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# tidyr (development version)

* `pivot_wider` now uses `.by` and `|>` syntax for the dplyr helper message to identify duplicates (@boshek, #1516)

# tidyr 1.3.0

## New features
Expand Down
5 changes: 2 additions & 3 deletions R/pivot-wide.R
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,8 @@ pivot_wider_spec <- function(data,
"*" = "Use `values_fn = list` to suppress this warning.",
"*" = "Use `values_fn = {{summary_fun}}` to summarise duplicates.",
"*" = "Use the following dplyr code to identify duplicates.",
" " = " {{data}} %>%",
" " = " dplyr::group_by({group_cols}) %>%",
" " = " dplyr::summarise(n = dplyr::n(), .groups = \"drop\") %>%",
" " = " {{data}} |>",
" " = " dplyr::summarise(n = dplyr::n(), .by = c({group_cols})) |>",
" " = " dplyr::filter(n > 1L)"
))
}
Expand Down
20 changes: 8 additions & 12 deletions tests/testthat/_snaps/pivot-wide.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@
* Use `values_fn = list` to suppress this warning.
* Use `values_fn = {summary_fun}` to summarise duplicates.
* Use the following dplyr code to identify duplicates.
{data} %>%
dplyr::group_by(a, key) %>%
dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
{data} |>
dplyr::summarise(n = dplyr::n(), .by = c(a, key)) |>
dplyr::filter(n > 1L)

# duplicated key warning mentions every applicable column
Expand All @@ -216,9 +215,8 @@
* Use `values_fn = list` to suppress this warning.
* Use `values_fn = {summary_fun}` to summarise duplicates.
* Use the following dplyr code to identify duplicates.
{data} %>%
dplyr::group_by(key) %>%
dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
{data} |>
dplyr::summarise(n = dplyr::n(), .by = c(key)) |>
dplyr::filter(n > 1L)
Output
# A tibble: 1 x 3
Expand All @@ -236,9 +234,8 @@
* Use `values_fn = list` to suppress this warning.
* Use `values_fn = {summary_fun}` to summarise duplicates.
* Use the following dplyr code to identify duplicates.
{data} %>%
dplyr::group_by(key) %>%
dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
{data} |>
dplyr::summarise(n = dplyr::n(), .by = c(key)) |>
dplyr::filter(n > 1L)
Output
# A tibble: 1 x 3
Expand All @@ -256,9 +253,8 @@
* Use `values_fn = list` to suppress this warning.
* Use `values_fn = {summary_fun}` to summarise duplicates.
* Use the following dplyr code to identify duplicates.
{data} %>%
dplyr::group_by(`a 1`, a2, `the-key`) %>%
dplyr::summarise(n = dplyr::n(), .groups = "drop") %>%
{data} |>
dplyr::summarise(n = dplyr::n(), .by = c(`a 1`, a2, `the-key`)) |>
dplyr::filter(n > 1L)

# values_fn is validated
Expand Down

0 comments on commit 4afb8ca

Please sign in to comment.