diff --git a/NEWS.md b/NEWS.md index 4a3e9dd6..ad79ee57 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/pivot-wide.R b/R/pivot-wide.R index b93c3412..7161fb7d 100644 --- a/R/pivot-wide.R +++ b/R/pivot-wide.R @@ -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)" )) } diff --git a/tests/testthat/_snaps/pivot-wide.md b/tests/testthat/_snaps/pivot-wide.md index aeae2e56..ebee7b21 100644 --- a/tests/testthat/_snaps/pivot-wide.md +++ b/tests/testthat/_snaps/pivot-wide.md @@ -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 @@ -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 @@ -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 @@ -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