Skip to content

Commit

Permalink
Ensure slice_*() family throw the right error when namespace prefix…
Browse files Browse the repository at this point in the history
…ed (#6955)

* Turn the expression into a string label early

* NEWS bullet
  • Loading branch information
DavisVaughan authored Nov 6, 2023
1 parent 3586473 commit bcfed14
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
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

0 comments on commit bcfed14

Please sign in to comment.