From d920ed7d50576aa93ccc09b2a8783dd557584916 Mon Sep 17 00:00:00 2001 From: Tina Rozsos <76408420+tinarozsos@users.noreply.github.com> Date: Mon, 10 Nov 2025 17:05:29 +0100 Subject: [PATCH 1/2] Remove weight column by uncount() when using .data pronoun --- R/uncount.R | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/R/uncount.R b/R/uncount.R index 797f4380..cd7cc04e 100644 --- a/R/uncount.R +++ b/R/uncount.R @@ -33,6 +33,7 @@ uncount.data.frame <- function(data, weights, ..., .remove = TRUE, .id = NULL) { check_name(.id, allow_null = TRUE) weights_quo <- enquo(weights) + weights_expr <- get_expr(weights_quo) w <- dplyr::pull(dplyr::mutate(data, `_weight` = !!weights_quo)) out <- vec_rep_each( @@ -44,8 +45,17 @@ uncount.data.frame <- function(data, weights, ..., .remove = TRUE, .id = NULL) { # NOTE it was decided to also remove grouping variables as there is no clear # best answer. See https://github.com/tidyverse/tidyr/pull/1070 - if (.remove && quo_is_symbol(weights_quo)) { - out[[as_string(get_expr(weights_quo))]] <- NULL + if (.remove) { + if (quo_is_symbol(weights_quo)) { + out[[as_string(weights_expr)]] <- NULL + } else if ( + # handles data masking + is.call(weights_expr) && + identical(weights_expr[[1]], as.name("$")) && + identical(weights_expr[[2]], as.name(".data")) + ) { + out[[as_string(weights_expr[[3]])]] <- NULL + } } if (!is.null(.id)) { From 7a16ac945c53ff8df52ad6280eb118e989ba661e Mon Sep 17 00:00:00 2001 From: Tina Rozsos <76408420+tinarozsos@users.noreply.github.com> Date: Mon, 10 Nov 2025 17:17:35 +0100 Subject: [PATCH 2/2] Add data masking test to uncount() --- tests/testthat/test-uncount.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testthat/test-uncount.R b/tests/testthat/test-uncount.R index 333e907b..75da0333 100644 --- a/tests/testthat/test-uncount.R +++ b/tests/testthat/test-uncount.R @@ -48,3 +48,8 @@ test_that("validates inputs", { uncount(df, x, .id = "") }) }) + +test_that("works with data masking", { + df <- tibble(x = 1, w = 2) + expect_equal(uncount(df, .data$w), uncount(df, w)) +})