Skip to content

Commit

Permalink
group_by() makes a shallow copy even with no groups.
Browse files Browse the repository at this point in the history
closes #4221
  • Loading branch information
romainfrancois committed Mar 4, 2019
1 parent b368e50 commit 895f6fc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# dplyr 0.8.0.9000

* Fixed mutate() on rowwise data frames with 0 rows (#4224).
* `group_by()` does a shallow copy even in the no groups case (#4221).

* Fixed `mutate()` on rowwise data frames with 0 rows (#4224).

* Fixed handling of bare formulas in colwise verbs (#4183).

Expand Down
11 changes: 6 additions & 5 deletions src/group_indices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,18 +640,19 @@ SymbolVector GroupedDataFrame::group_vars() const {

// [[Rcpp::export]]
DataFrame grouped_df_impl(DataFrame data, SymbolVector symbols, bool drop) {
DataFrame copy(shallow_copy(data));

if (!symbols.size()) {
GroupedDataFrame::strip_groups(data);
data.attr("class") = NaturalDataFrame::classes();
return data;
GroupedDataFrame::strip_groups(copy);
copy.attr("class") = NaturalDataFrame::classes();
return copy;
}

DataFrame copy(shallow_copy(data));
set_class(copy, GroupedDataFrame::classes());

// we've made a copy and we are about to create the groups
// attribute, so we make sure there is no more a vars
// attribute lurking around from the pore 0.8.0 area
// attribute lurking around from the pre 0.8.0 area
copy.attr("vars") = R_NilValue;
copy.attr("drop") = R_NilValue;

Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-group-by.r
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,10 @@ test_that("group_by(add = TRUE) sets .drop if the origonal data was .drop", {
expect_equal(n_groups(res), 1L)
expect_true(group_drops(res))
})

test_that("group_by() makes a shallow copy of data even in the corner case", {
df <- data.frame(x = 1:4)
gdf <- group_by(df)
expect_true(inherits(gdf, "tbl_df"))
expect_false(inherits(df, "tbl_df"))
})

0 comments on commit 895f6fc

Please sign in to comment.