-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Allow negative n/prop in slice_sample() #6405
Conversation
Conflicts: R/slice.R
To avoid near future merge conflict
@@ -423,48 +415,57 @@ check_slice_n_prop <- function(n, prop, error_call = caller_env()) { | |||
list(type = "n", n = 1L) | |||
} else if (!missing(n) && missing(prop)) { | |||
n <- check_constant(n, "n", error_call = error_call) | |||
if (!is.numeric(n) || length(n) != 1 || is.na(n)) { | |||
abort("`n` must be a single number.", call = error_call) | |||
if (!is_integerish(n, n = 1) || is.na(n)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!is_integerish(n, n = 1) || is.na(n)) { | |
if (!is_integerish(n, n = 1, finite = TRUE)) { |
This catches NA
and NaN
along with Inf/-Inf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can also just use check_number()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inf
is explicitly allowed in the tests 😬
abort( | ||
glue("`n` must be a round number, not {obj_type_friendly(n)}."), | ||
call = error_call | ||
) | ||
} | ||
list(type = "n", n = n) | ||
} else if (!missing(prop) && missing(n)) { | ||
prop <- check_constant(prop, "prop", error_call = error_call) | ||
if (!is.numeric(prop) || length(prop) != 1 || is.na(prop)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (!is.numeric(prop) || length(prop) != 1 || is.na(prop)) { | |
if (!is_integerish(prop, n = 1, finite = TRUE)) { |
eh?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prop isn't supposed to be an integer 😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh duh
Fixes #6402
This seems reasonable to me, and already in alignment with what the docs suggest — negative
prop
andn
drop rows.