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

Prefer dplyr_col_select() over [ to retain attributes in distinct() #6344

Merged
merged 3 commits into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -1,5 +1,7 @@
# dplyr (development version)

* `distinct()` now retains attributes of bare data frames (#6318).

* dplyr no longer provides `count()` and `tally()` methods for `tbl_sql`.
These methods have been accidentally overriding the `tbl_lazy` methods that
dbplyr provides, which has resulted in issues with the grouping structure of
Expand Down
9 changes: 6 additions & 3 deletions R/distinct.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,14 @@ distinct.data.frame <- function(.data, ..., .keep_all = FALSE) {
caller_env = caller_env()
)

# out <- as_tibble(prep$data)
out <- prep$data
loc <- vec_unique_loc(as_tibble(out)[prep$vars])

dplyr_row_slice(out[prep$keep], loc)
cols <- dplyr_col_select(out, prep$vars)
loc <- vec_unique_loc(cols)

out <- dplyr_col_select(out, prep$keep)

DavisVaughan marked this conversation as resolved.
Show resolved Hide resolved
dplyr_row_slice(out, loc)
}


Expand Down
10 changes: 10 additions & 0 deletions tests/testthat/test-distinct.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ test_that("distinct() propagates caller env", {
expect_caller_env(distinct(mtcars, sig_caller_env()))
})

test_that("distinct() preserves attributes on bare data frames (#6318)", {
df <- vctrs::data_frame(x = c(1, 1))
attr(df, "foo") <- "bar"

out <- distinct(df, x)
expect_identical(attr(out, "foo"), "bar")

out <- distinct(df, y = x + 1L)
expect_identical(attr(out, "foo"), "bar")
})

# Errors ------------------------------------------------------------------

Expand Down