I'm sorry if I'm not supposed to post something here that's not an
answer, but my post is too long for a comment.
I'd like to point out that janosdivenyi's solution of adding a
trailing []
to dt
does not always give the expected results (even
when using data.table 1.9.6 or 1.10.4) as I do below.
The examples below show that if dt
is the last line in the function
one gets the desired behaviour without the presence of the
trailing []
, but if dt
is not on the last line in the function then
a trailing []
is needed to get the desired behaviour.
The first example shows that with no trailing []
on dt
we get the
expected behaviour when dt
is on the last line of the function
mydt <- data.table(x = 1:3, y = 5:7)
myfunction <- function(dt) {
df <- 1
dt[, z := y - x]
}
myfunction(mydt) # Nothing printed as expected
mydt # Content printed as desired
## x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4
Adding a trailing []
on dt
gives unexpected behaviour
mydt <- data.table(x = 1:3, y = 5:7)
myfunction <- function(dt) {
df <- 1
dt[, z := y - x][]
}
myfunction(mydt) # Content printed unexpectedly
## x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4
mydt # Content printed as desired
## x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4
Moving df <- 1
to after the dt with no trailing []
gives unexpected
behaviour
mydt <- data.table(x = 1:3, y = 5:7)
myfunction <- function(dt) {
dt[, z := y - x]
df <- 1
}
myfunction(mydt) # Nothing printed as expected
mydt # Nothing printed unexpectedly
Moving df <- 1
after the dt with a trailing []
gives the expected
behaviour
mydt <- data.table(x = 1:3, y = 5:7)
myfunction <- function(dt) {
dt[, z := y - x][]
df <- 1
}
myfunction(mydt) # Nothing printed as expected
mydt # Content printed as desired
## x y z
## 1: 1 5 4
## 2: 2 6 4
## 3: 3 7 4
DT[]
is only needed when print of data.table is suppressed, so when using:=
orset*
functions – Strove