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

Allow negative n/prop in slice_sample() #6405

Merged
merged 11 commits into from
Aug 18, 2022
Merged

Allow negative n/prop in slice_sample() #6405

merged 11 commits into from
Aug 18, 2022

Conversation

hadley
Copy link
Member

@hadley hadley commented Aug 16, 2022

Fixes #6402

This seems reasonable to me, and already in alignment with what the docs suggest — negative prop and n drop rows.

@hadley hadley requested review from eutwt and DavisVaughan August 16, 2022 21:14
@hadley hadley marked this pull request as ready for review August 16, 2022 21:14
R/slice.R Outdated Show resolved Hide resolved
R/slice.R Outdated Show resolved Hide resolved
R/slice.R Outdated Show resolved Hide resolved
@hadley hadley requested a review from DavisVaughan August 18, 2022 15:51
@@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Copy link
Member

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()

Copy link
Member Author

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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!is.numeric(prop) || length(prop) != 1 || is.na(prop)) {
if (!is_integerish(prop, n = 1, finite = TRUE)) {

eh?

Copy link
Member Author

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 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh duh

@hadley hadley merged commit ee313a6 into main Aug 18, 2022
@hadley hadley deleted the slice-sample-negative branch August 18, 2022 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

slice_*: argument n and prop can be negative for all but slice_sample?
3 participants