From d6f801b5d015f8a7fbaedde66bdd694ce8794fd4 Mon Sep 17 00:00:00 2001 From: apalacio9502 Date: Sun, 2 Jun 2024 18:46:23 -0500 Subject: [PATCH 1/3] mutate() now supports the arguments .keep = "transmute" --- R/verb-mutate.R | 10 +++++++--- man/mutate.tbl_lazy.Rd | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/R/verb-mutate.R b/R/verb-mutate.R index c5cc1cce0..119fbf30b 100644 --- a/R/verb-mutate.R +++ b/R/verb-mutate.R @@ -24,7 +24,7 @@ mutate.tbl_lazy <- function(.data, ..., .by = NULL, - .keep = c("all", "used", "unused", "none"), + .keep = c("all", "used", "unused", "none", "transmute"), .before = NULL, .after = NULL) { keep <- arg_match(.keep) @@ -53,6 +53,7 @@ mutate.tbl_lazy <- function(.data, out <- mutate_relocate( out = out, + keep = keep, before = {{ .before }}, after = {{ .after }}, names_original = names_original @@ -238,11 +239,11 @@ get_mutate_dot_cols <- function(quosures, all_vars) { ) } -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) } @@ -264,6 +265,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) } else { names_keep <- switch( keep, diff --git a/man/mutate.tbl_lazy.Rd b/man/mutate.tbl_lazy.Rd index 35f4db4d1..27bbc6a9a 100644 --- a/man/mutate.tbl_lazy.Rd +++ b/man/mutate.tbl_lazy.Rd @@ -8,7 +8,7 @@ .data, ..., .by = NULL, - .keep = c("all", "used", "unused", "none"), + .keep = c("all", "used", "unused", "none", "transmute"), .before = NULL, .after = NULL ) @@ -38,6 +38,8 @@ columns. This is useful if you generate new columns, but no longer need the columns used to generate them. \item \code{"none"} doesn't retain any extra columns from \code{.data}. Only the grouping variables and columns created by \code{...} are kept. +\item \code{"transmute"} equivalent to "none", but preserves the column order in the +mutate. }} \item{.before, .after}{<\code{\link[dplyr:dplyr_tidy_select]{tidy-select}}> Optionally, control where new columns From a769d790a1c7de125599a9150debe89156417ecc Mon Sep 17 00:00:00 2001 From: apalacio9502 Date: Sun, 2 Jun 2024 18:53:58 -0500 Subject: [PATCH 2/3] Update news --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index dae72d35d..938b03528 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ * `across(everything())` doesn't select grouping columns created via `.by` in `summarise()` (@mgirlich, #1493). + +* `mutate()` now supports the argument `.keep = "transmute"`. # dbplyr 2.5.0 From f6c1f788db3ec2161b87f2327f690b76e76fd787 Mon Sep 17 00:00:00 2001 From: apalacio9502 Date: Sun, 2 Jun 2024 20:39:44 -0500 Subject: [PATCH 3/3] dplyr (>= 1.1.4.9000) is required --- DESCRIPTION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index fb1835022..fa84119cb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,7 +22,7 @@ Imports: blob (>= 1.2.0), cli (>= 3.6.1), DBI (>= 1.1.3), - dplyr (>= 1.1.2), + dplyr (>= 1.1.4.9000), glue (>= 1.6.2), lifecycle (>= 1.0.3), magrittr,