Skip to content
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
14 changes: 12 additions & 2 deletions R/uncount.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)) {
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-uncount.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Loading