Skip to content

Commit

Permalink
updated doc of env
Browse files Browse the repository at this point in the history
  • Loading branch information
nitish jha authored and nitish jha committed Aug 6, 2024
1 parent bad266b commit 74f0da4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion man/data.table.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ data.table(\dots, keep.rownames=FALSE, check.names=FALSE, key=NULL, stringsAsFac
See examples as well as \href{../doc/datatable-secondary-indices-and-auto-indexing.html}{\code{vignette("datatable-secondary-indices-and-auto-indexing")}}.
}
\item{env}{ List or an environment, passed to \code{\link{substitute2}} for substitution of parameters in \code{i}, \code{j} and \code{by} (or \code{keyby}). Use \code{verbose} to preview constructed expressions. For more details see \href{../doc/datatable-programming.html}{\code{vignette("datatable-programming")}}. }
\item{env}{ List or an environment, passed to \code{\link{substitute2}} for substitution of parameters in \code{i}, \code{j} and \code{by} (or \code{keyby}). For function names, you can use them as strings (e.g., \code{"sum"}) or pass function objects directly (e.g., \code{sum}). Use \code{verbose} to preview constructed expressions. For more details, see \href{../doc/datatable-programming.html}{\code{vignette("datatable-programming")}}.}
\item{showProgress}{ \code{TRUE} shows progress indicator with estimated time to completion for lengthy "by" operations. }
}
Expand Down Expand Up @@ -403,6 +403,15 @@ print(DT["b", v2:=84L, on="x"]) # subassign to new column by reference (NA

DT[, m:=mean(v), by=x][] # add new column by reference by group
# NB: postfix [] is shortcut to print()

# Injecting function into env parameter
DT <- data.table(a = rep(1:2, each = 5), b = 1:10)

DT[, f(b), by = a, env = list(f = "sum")] # Use function name as a string
DT[, f(b), by = a, env = list(f = "sum"), verbose = TRUE] # Preview function name injection
DT[, f(b), by = a, env = list(f = sum)] # Use function object directly
DT[, f(b), by = a, env = list(f = sum), verbose = TRUE] # Preview function object injection

# advanced usage
DT = data.table(x=rep(c("b","a","c"),each=3), v=c(1,1,1,2,2,1,1,2,2), y=c(1,3,6), a=1:9, b=9:1)

Expand Down

0 comments on commit 74f0da4

Please sign in to comment.