Rework mutate.data.frame()
to better implement .keep
#6035
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #6007
Closes #5967
I'm hoping that this is a much simpler variant of #6007 that is overall easier to understand and test.
Essentially there are 4 rules that always hold:
.before
and.after
. Default is to add them to the end.This takes care of everything except
.keep
, and we now have a language to refer to the different types of columns. There are two extra column types:Now for
.keep
:"all"
:"used"
:"unused"
:"none"
:So the implementation is:
dplyr_col_modify()
add modified and new variables to the data frame. This works nicely as modified variables overwrite without changing the location, and new variables are added to the end.relocate()
change the position of the new variables as needed..keep
rules outlined above to figure out what to drop from the set of used/unused variables. Note that the column order doesn't change at all here. This is strictly about dropping columns.An important invariant that falls out here is that
.keep
plays no role in the column ordering, and I think that is valuable. I think givingkeep = "none"
special behavior in a few places that changed column order is what made this so hard to get correct before.