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

.keep = "transmute" is implemented in mutate. #7038

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* R >=3.6.0 is now explicitly required (#7026).

* The new equivalent of `transmute()` is `mutate(.keep = "transmute")`. This is analogous to `mutate(.keep = "none")`, but it retains the column order within mutate (#6861).

# dplyr 1.1.4

* `join_by()` now allows its helper functions to be namespaced with `dplyr::`,
Expand Down
14 changes: 10 additions & 4 deletions R/mutate.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@
#' the columns used to generate them.
#' * `"none"` doesn't retain any extra columns from `.data`. Only the grouping
#' variables and columns created by `...` are kept.
#' * `"transmute"` equivalent to "none", but preserves the column order in the
#' mutate.
#' @param .before,.after
#' <[`tidy-select`][dplyr_tidy_select]> Optionally, control where new columns
#' should appear (the default is to add to the right hand side). See
Expand All @@ -171,10 +173,10 @@
mutate.data.frame <- function(.data,
...,
.by = NULL,
.keep = c("all", "used", "unused", "none"),
.keep = c("all", "used", "unused", "none", "transmute"),
.before = NULL,
.after = NULL) {
keep <- arg_match0(.keep, values = c("all", "used", "unused", "none"))
keep <- arg_match0(.keep, values = c("all", "used", "unused", "none", "transmute"))

by <- compute_by({{ .by }}, .data, by_arg = ".by", data_arg = ".data")

Expand All @@ -187,6 +189,7 @@

out <- mutate_relocate(
out = out,
keep = keep,
before = {{ .before }},
after = {{ .after }},
names_original = names_original
Expand All @@ -208,11 +211,11 @@

# Helpers -----------------------------------------------------------------

mutate_relocate <- function(out, before, after, names_original) {
mutate_relocate <- function(out, keep, before, after, names_original) {
before <- enquo(before)
after <- enquo(after)

if (quo_is_null(before) && quo_is_null(after)) {
if (keep == "transmute" || (quo_is_null(before) && quo_is_null(after))) {
return(out)
}

Expand All @@ -234,6 +237,9 @@

if (keep == "all") {
names_out <- names
} else if (keep == "transmute") {
names_groups <- setdiff(names_groups, names_new)
names_out <- c(names_groups, names_new)

Check warning on line 242 in R/mutate.R

View check run for this annotation

Codecov / codecov/patch

R/mutate.R#L241-L242

Added lines #L241 - L242 were not covered by tests
} else {
names_keep <- switch(
keep,
Expand Down
4 changes: 2 additions & 2 deletions R/transmute.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#'
#' `transmute()` creates a new data frame containing only the specified
#' computations. It's superseded because you can perform the same job
#' with `mutate(.keep = "none")`.
#' with `mutate(.keep = "transmute")`.
#'
#' @inheritParams mutate
#' @section Methods:
Expand All @@ -29,7 +29,7 @@
#' @export
transmute <- function(.data, ...) {
# dplyr 1.1.0
lifecycle::signal_stage("superseded", "transmute()", I("mutate(.keep = 'none')"))
lifecycle::signal_stage("superseded", "transmute()", I("mutate(.keep = 'transmute')"))

UseMethod("transmute")
}
Expand Down
4 changes: 3 additions & 1 deletion man/mutate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/transmute.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading