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

Ensure slice_*() family throw the right error when namespace prefixed #6955

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# dplyr (development version)

* `slice_*()` now throw the correct error if you forget to name `n` while also
prefixing the call with `dplyr::` (#6946).

* `join_by()` now allows its helper functions to be namespaced with `dplyr::`,
like `join_by(dplyr::between(x, lower, upper))` (#6838).

Expand Down
1 change: 1 addition & 0 deletions R/slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ check_slice_unnamed_n_prop <- function(..., n, prop, error_call = caller_env())

if (length(dots) == 1L && names2(dots)[[1L]] == "") {
slice_call <- frame_call(frame = error_call)[[1]]
slice_call <- as_label(slice_call)
bullets <- c(
"`n` must be explicitly named.",
i = glue("Did you mean `{slice_call}(n = {as_label(dots[[1]])})`?")
Expand Down
33 changes: 33 additions & 0 deletions tests/testthat/_snaps/slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,39 @@
! `n` must be explicitly named.
i Did you mean `slice_sample(n = 5)`?

---

Code
dplyr::slice_head(df, 5)
Condition
Error in `dplyr::slice_head()`:
! `n` must be explicitly named.
i Did you mean `dplyr::slice_head(n = 5)`?
Code
dplyr::slice_tail(df, 5)
Condition
Error in `dplyr::slice_tail()`:
! `n` must be explicitly named.
i Did you mean `dplyr::slice_tail(n = 5)`?
Code
dplyr::slice_min(df, x, 5)
Condition
Error in `dplyr::slice_min()`:
! `n` must be explicitly named.
i Did you mean `dplyr::slice_min(n = 5)`?
Code
dplyr::slice_max(df, x, 5)
Condition
Error in `dplyr::slice_max()`:
! `n` must be explicitly named.
i Did you mean `dplyr::slice_max(n = 5)`?
Code
dplyr::slice_sample(df, 5)
Condition
Error in `dplyr::slice_sample()`:
! `n` must be explicitly named.
i Did you mean `dplyr::slice_sample(n = 5)`?

---

Code
Expand Down
11 changes: 10 additions & 1 deletion tests/testthat/test-slice.R
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ test_that("slice_*() doesn't look for `n` in data (#6089)", {

test_that("slice_*() checks that `n=` is explicitly named and ... is empty", {
# i.e. that every function calls check_slice_dots()

df <- data.frame(x = 1:10)

expect_snapshot(error = TRUE, {
slice_head(df, 5)
slice_tail(df, 5)
Expand All @@ -330,6 +330,15 @@ test_that("slice_*() checks that `n=` is explicitly named and ... is empty", {
slice_sample(df, 5)
})

# And works with namespace prefix (#6946)
expect_snapshot(error = TRUE, {
dplyr::slice_head(df, 5)
dplyr::slice_tail(df, 5)
dplyr::slice_min(df, x, 5)
dplyr::slice_max(df, x, 5)
dplyr::slice_sample(df, 5)
})

expect_snapshot(error = TRUE, {
slice_head(df, 5, 2)
slice_tail(df, 5, 2)
Expand Down
Loading