diff --git a/NEWS.md b/NEWS.md index 6ce883c568..12c05d4dff 100644 --- a/NEWS.md +++ b/NEWS.md @@ -30,6 +30,8 @@ 3. In v1.12.4, fractional `fread(..., stringsAsFactors=)` was added. For example if `stringsAsFactors=0.2`, any character column with fewer than 20% unique strings would be cast as `factor`. This is now documented in `?fread` as well, [#4706](https://github.com/Rdatatable/data.table/issues/4706). Thanks to @markderry for the PR. +4. `cube(DT, by="a")` now gives a more helpful error that `j` is missing, [#4282](https://github.com/Rdatatable/data.table/pull/4282). + # data.table [v1.14.0](https://github.com/Rdatatable/data.table/milestone/23?closed=1) (21 Feb 2021) diff --git a/R/groupingsets.R b/R/groupingsets.R index 6281615dd5..5c3ad02d4b 100644 --- a/R/groupingsets.R +++ b/R/groupingsets.R @@ -27,10 +27,12 @@ cube.data.table = function(x, j, by, .SDcols, id = FALSE, ...) { stop("Argument 'by' must be a character vector of column names used in grouping.") if (!is.logical(id)) stop("Argument 'id' must be a logical scalar.") + if (missing(j)) + stop("Argument 'j' is required") # generate grouping sets for cube - power set: http://stackoverflow.com/a/32187892/2490497 n = length(by) keepBool = sapply(2L^(seq_len(n)-1L), function(k) rep(c(FALSE, TRUE), times=k, each=((2L^n)/(2L*k)))) - sets = lapply((2L^n):1L, function(j) by[keepBool[j, ]]) + sets = lapply((2L^n):1L, function(jj) by[keepBool[jj, ]]) # redirect to workhorse function jj = substitute(j) groupingsets.data.table(x, by=by, sets=sets, .SDcols=.SDcols, id=id, jj=jj) diff --git a/inst/tests/tests.Rraw b/inst/tests/tests.Rraw index dfeb47841f..4535a6e048 100644 --- a/inst/tests/tests.Rraw +++ b/inst/tests/tests.Rraw @@ -17295,3 +17295,6 @@ test(2165.3, X[Y], data.table(A=2:3, B=2:3, i.A=2:1, ke test(2165.4, X[Y, on=.(A)], data.table(A=2:1, B=c(2L,NA), i.B=2:3)) # no key test(2165.5, X[Y, on=.(A), x.B, by=.EACHI], data.table(A=2:1, x.B=c(2L,NA))) # no key +# missing j was caught in groupingsets but not cube, leading to unexpected error message, #4282 +DT = data.table(a=1) +test(2166, cube(DT, by='a'), error="Argument 'j' is required")