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

select() updates order_var #1106

Merged
merged 12 commits into from
Feb 14, 2023
Merged

select() updates order_var #1106

merged 12 commits into from
Feb 14, 2023

Conversation

mgirlich
Copy link
Collaborator

Fixes #1103.
I had to slightly adapt my initial proposal to allow symbols and desc(<symbol>). Note that this is only enforced in window_order() but not in add_order(). Without this the query of slice_sample() would have been quite a bit more complicated. It is a bit of a hack but as add_order() is an internal function this should be fine.

lf %>% window_order(desc(y)) %>% select(y2 = y) %>% op_sort(),
list(expr(desc(y2)))
)
# keeps sort order
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the order of variables shouldn't be touched by select()

Copy link
Member

Choose a reason for hiding this comment

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

Agreed

test_that("select handles order vars", {
lf <- lazy_frame(x = 1, y = 1, z = 1)
# can drop order vars
expect_equal(lf %>% window_order(y) %>% select(-y) %>% op_sort(), list())
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is different to the handling of grouping variables but keeping them is probably unwanted and unexpected. Also, this would otherwise probably be a big breaking change.

@mgirlich mgirlich requested a review from hadley January 20, 2023 09:00
@mgirlich mgirlich added this to the 2.3.1 milestone Feb 2, 2023
@mgirlich
Copy link
Collaborator Author

mgirlich commented Feb 3, 2023

@hadley You're now happy with the PR and only forgot to approve? Or do you need to take a closer look? 😉

@mgirlich mgirlich mentioned this pull request Feb 10, 2023
19 tasks
R/verb-select.R Outdated Show resolved Hide resolved

desc <- purrr::map_lgl(order, ~ is_call(.x, "desc", n = 1L))
order <- purrr::map_if(order, desc, ~ call_args(.x)[[1L]])
order <- purrr::map_chr(order, as_name)
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't check_window_order_dots() ensure that this is already a symbol?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't quite follow. check_window_order_dots() ensures that each element of order is either a symbol or desc(<symbol>). And these two steps extract the underlying symbol and convert it to a string so that it can be mapped more easily.

Copy link
Member

Choose a reason for hiding this comment

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

When is an element of order not already going to be a name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

After line 175 every element of order is a symbol. Then as_name() converts the symbols to strings.

Copy link
Member

Choose a reason for hiding this comment

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

Oooooh, I was confused by as_name() being very different to as.name()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I just looked into the documentation

rlang::as_name() is the opposite of base::as.name()

which is indeed quite mean 😄

window_order({{order_by}}) %>%
filter(!!window_fun) %>%
# must use `add_order()` as `window_order()` only allows variables
.data$lazy_query <- add_order(.data, quos({{order_by}}))
Copy link
Member

Choose a reason for hiding this comment

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

Does any existing test cover this? What happens if there's a select after this step?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The previous window order is restored below and there is a test for this. A select() after this step would drop the window order in case of slice_sample() and keep the window order for the others (unless the window order variables are de-selected).

@mgirlich mgirlich merged commit 81312d9 into main Feb 14, 2023
@mgirlich mgirlich deleted the window-order-removed-var branch February 14, 2023 04:20
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.

window_order crashing when deleting the variable that I am ordering for
2 participants