Skip to content

Commit

Permalink
Merge pull request tidyverse#2637 from lionel-/fix-summarised-symbols
Browse files Browse the repository at this point in the history
Fix hybrid handling of summarised symbols
  • Loading branch information
lionel- authored Apr 12, 2017
2 parents 832b1cb + b66864b commit 22504ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/hybrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,13 @@ class VariableResult : public Result {
VariableResult(const ILazySubsets& subsets_, const SymbolString& name_) : subsets(subsets_), name(name_) {}

SEXP process(const GroupedDataFrame& gdf) {
check_length(gdf.max_group_size(), 1, "a summary value", name);
return process(*gdf.group_begin());
if (subsets.is_summary(name)) {
// No need to check length since the summary has already been checked
return subsets.get_variable(name);
} else {
check_length(gdf.max_group_size(), 1, "a summary value", name);
return process(*gdf.group_begin());
}
}

SEXP process(const RowwiseDataFrame&) {
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-summarise.r
Original file line number Diff line number Diff line change
Expand Up @@ -947,3 +947,8 @@ test_that("proper handling of NA factors (#2588)", {
ret <- df %>% group_by(x) %>% summarise(y = y[1])
expect_identical(as.character(ret$y), c(NA, NA, "3"))
})

test_that("can refer to previously summarised symbols", {
expect_identical(summarise(group_by(mtcars, cyl), x = 1, z = x)[2:3], tibble(x = c(1, 1, 1), z = x))
expect_identical(summarise(group_by(mtcars, cyl), x = n(), z = x)[2:3], tibble(x = c(11L, 7L, 14L), z = x))
})

0 comments on commit 22504ec

Please sign in to comment.