withDT: Use data.table syntax for one call

Description Usage Arguments Details Examples

View source: R/withDT.R

Description

Use data.table syntax for one call

Usage

1
withDT(expr, lock = getOption("withDT.lock"))

Arguments

expr

an expression where [ will forward its arguments to data.Table:::`[.data.table` if x inherits from data.frame

lock

wether to lock the intermediate data.table so syntax of assignment by reference is forbidden.

Details

An important particularity of withDT() is that assignments are never done by reference. Though limiting it avoids the confusion and unintended behaviors that might come with them. The syntax of these assignments is still supported but will return a copy. In order to fail explicitly whenever that syntax is used, the argument lock can be set to TRUE.

Other benefits of this function are :

Some caveats are:

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
iris2 <- withDT(iris[, .(meanSW = mean(Sepal.Width)), by = Species][,cbind(.SD, a =3)])
iris2
class(iris2)
# can be also done as follows, which wouldn't have the same output with standard
# data.table code due to assignment by reference
iris3 <- withDT(iris[, .(meanSW = mean(Sepal.Width)), by = Species][,a:=3])
identical(iris2,iris3)
# iris wasn't modified
class(iris)
names(iris)
# but wouldn't work with lock == TRUE
try(iris4 <- withDT(lock=TRUE,iris[, .(meanSW = mean(Sepal.Width)), by = Species][,b:=3]))

moodymudskipper/withDT documentation built on Nov. 4, 2019, 7:29 p.m.