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

Factor out eval_relocate() and use dplyr_col_select() in relocate() #6342

Merged
merged 3 commits into from
Aug 9, 2022

Conversation

DavisVaughan
Copy link
Member

@DavisVaughan DavisVaughan commented Jul 20, 2022

Closes #5829 (but we'd still need to move this to tidyselect)
Closes #6167
Closes #6341

This PR pulls eval_relocate() out of relocate.data.frame(). The eventual goal would be to move this to tidyselect for other backends to use it (#5829) but this is a good first step.

The before and after arguments of eval_relocate() take defused input, or NULL, which feels right given that expr works basically the same way.

eval_relocate() returns a named integer vector the same size as ncol(data) (important invariant) that describes how to rearrange the columns of data. The names are normally the same as the original data, but renaming can happen in expr so it is possible for them to be different.

I fixed a few edge case bugs in the implementation of eval_relocate(). Mainly around 0-col data frames and what happens when you supply .before or .after but they result in an empty selection.

I've also started using dplyr_col_select() in relocate.data.frame() rather than just .data[pos]. This solves #6341 and probably a few other consistency bugs since dplyr_col_select() has a few extra sanity checks in it.

@DavisVaughan
Copy link
Member Author

@lionel- would you mind looking at this since it would probably end up in tidyselect (r-lib/tidyselect#232)? I think it is ok if it is not 100% equivalent to what you'd implement for eval_relocate(), but I think this gets pretty close.

@DavisVaughan DavisVaughan requested a review from lionel- July 20, 2022 18:20
@DavisVaughan DavisVaughan force-pushed the feature/eval-relocate branch from 04d2b92 to 0224ecb Compare July 20, 2022 18:23
has_after <- !quo_is_null(.after)
n <- length(data)

before <- as_quosure(before, env = env)
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit on the fence about whether it'd be better to use enquo() for these arguments. But it seems better to be consistent and only take strict arguments in an eval_ function, so I think this is a good solution.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yea I was trying to be consistent with expr here, since eval_select() captures expr in the same way with this as_quosure() idea.

I started with enquo() but it felt really strange. This felt better to me

R/relocate.R Show resolved Hide resolved
R/relocate.R Outdated
}

if (has_before) {
# TODO: Use `allow_rename = FALSE`. https://github.com/r-lib/tidyselect/issues/225
Copy link
Member

Choose a reason for hiding this comment

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

This is fixed in the dev version which I plan to release right before the dplyr release.

Copy link
Member Author

Choose a reason for hiding this comment

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

But this bug still exists :/ r-lib/tidyselect#221

Copy link
Member

Choose a reason for hiding this comment

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

I've added this to my list of issues to fix for the dplyr release.

@DavisVaughan DavisVaughan force-pushed the feature/eval-relocate branch from 5862246 to b31a925 Compare August 9, 2022 21:14
@DavisVaughan DavisVaughan merged commit ed2efb3 into tidyverse:main Aug 9, 2022
@DavisVaughan DavisVaughan deleted the feature/eval-relocate branch August 9, 2022 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants