Skip to content

Commit

Permalink
.keep = "transmute" is implemented in mutate.
Browse files Browse the repository at this point in the history
  • Loading branch information
apalacio9502 committed Jun 2, 2024
1 parent be36acf commit 33e6455
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
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 @@ mutate <- function(.data, ...) {
#' 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 <- function(.data, ...) {
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 @@ mutate.data.frame <- function(.data,

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

# 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 @@ mutate_keep <- function(out, keep, used, names_new, names_groups) {

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.

0 comments on commit 33e6455

Please sign in to comment.