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

Reindexing grouped data frames only update class if not grouped already #3438

Merged
merged 3 commits into from
Mar 20, 2018
Merged
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion src/group_indices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,18 @@ DataFrame build_index_cpp(DataFrame data) {
biggest_group = std::max(biggest_group, (int)chunk.size());
}

// The attributes are injected into data without duplicating it!
// The object is mutated, violating R's usual copy-on-write semantics.
// This is safe here, because the indices are an auxiliary data structure
// that is rebuilt as necessary. Updating the object in-place saves costly
// recomputations, but we need to be careful with the "class" attribute.
data.attr("indices") = indices;
data.attr("group_sizes") = group_sizes;
data.attr("biggest_group_size") = biggest_group;
data.attr("labels") = labels;
set_class(data, CharacterVector::create("grouped_df", "tbl_df", "tbl", "data.frame"));
if (r_match(CharacterVector("grouped_df"), get_class(data))[0] < 0) {
set_class(data, CharacterVector::create("grouped_df", "tbl_df", "tbl", "data.frame"));
}
return data;
}

Expand Down