Skip to content

Commit d68b7fb

Browse files
Update groupingsets.Rd
1 parent 33b60cc commit d68b7fb

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

man/groupingsets.Rd

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ groupingsets(x, \dots)
3434
The \code{label} argument can be a named list of scalars, or a scalar, or \code{NULL}. When \code{label} is a list, each element name must be (1) a variable name in \code{by}, or (2) the first element of the class in the data.table \code{x} of a variable in \code{by}, or (3) one of 'character', 'integer', 'numeric', 'factor', 'Date', 'IDate'. The order of the list elements is not important. A label specified by variable name will apply only to that variable, while a label specified by first element of a class will apply to all variables in \code{by} for which the first element of the class of the variable in \code{x} matches the \code{label} element name, except for variables that have a label specified by variable name (that is, specification by variable name takes precedence over specification by class). For \code{label} elements with name in \code{by}, the class of the label value must be the same as the class of the variable in \code{x}. For \code{label} elements with name not in \code{by}, the first element of the class of the label value must be the same as the \code{label} element name. For example, \code{label = list(integer = 999, IDate = as.Date("3000-01-01"))} would produce an error because \code{class(999)[1]} is not \code{"integer"} and \code{class(as.Date("3000-01-01"))[1]} is not \code{"IDate"}. A corrected specification would be \code{label = list(integer = 999L, IDate = as.IDate("3000-01-01"))}.
3535

3636
The \code{label = <scalar>} option provides a shorter alternative in the case where only one class of grouping variable requires a label. For example, \code{label = list(character = "Total")} can be shortened to \code{label = "Total"}. When this option is used, the label will be applied to all variables in \code{by} for which the first element of the class of the variable in \code{x} matches the first element of the class of the scalar.
37-
38-
The \code{jj} argument provides a mechanism to handle scoping issues when working with variables from outer environments. When using \code{j} directly with variables not defined in the columns of the data.table, R's evaluation rules may not find these variables, resulting in "object not found" errors. The \code{jj} argument accepts a quoted expression which can be created with either \code{quote()} (for direct calls referencing only columns and special symbols) or \code{substitute()} (when inside functions that need to capture parameters from the outer environment). For reliable behavior with function parameters, especially in nested environments, use the two-argument form \code{substitute(expr, list(param=value))} which binds values rather than symbols. This approach is particularly useful in functions where parameters need to be referenced within aggregation expressions. When \code{jj} is provided, the function will ignore any value specified in the \code{j} argument.
3937
}
4038
\value{
4139
A data.table with various aggregates.
@@ -95,16 +93,13 @@ groupingsets(DT, jj = quote(list(count = .N, total = sum(value))),
9593
by = c("color","year","status"), # jj with direct column references
9694
sets = list("color", c("year","status"), character()), id = TRUE)
9795

98-
# Example showing when substitute() is necessary - in a wrapper function
99-
# Note: Use substitute(expr, list(param=value)) to bind values, not symbols
100-
custom_grouping <- function(DT, multiplier) {
101-
# use substitute() with environment to ensure variable are properly evaluated
102-
groupingsets(DT, jj = substitute(list(count = .N, total = sum(value) * m),
103-
list(m = multiplier)),
96+
# Wrapper around groupingsets demonstrating NSE with user-defined j expression
97+
mygrp = function(x, j) {
98+
groupingsets(x, jj = substitute(j),
10499
by = c("color", "year", "status"),
105100
sets = list("color", c("year", "status"), character()), id = TRUE)
106101
}
107-
# Using the wrapper function
108-
local({ foo <- 3; custom_grouping(DT, multiplier = foo) })
102+
multiplier = 2
103+
mygrp(DT, .(count = .N, total = sum(value) * multiplier))
109104
}
110105
\keyword{ data }

0 commit comments

Comments
 (0)