I am in the process of creating a package that uses a data.table
as a dataset and has a couple of functions which assign by reference using :=
.
I have built a simple package to demonstrate my problem
library(devtools)
install_github('foo','mnel')
It contains two functions
foo <- function(x){
x[, a := 1]
}
fooCall <- function(x){
eval(substitute(x[, a :=1]),parent.frame(1))
}
and a dataset (not lazy loaded) DT
, created using
DT <- data.table(b = 1:5)
save(DT, file = 'data/DT.rda')
When I install this package, my understanding is that foo(DT)
should assign by reference within DT
.
library(foo)
data(DT)
foo(DT)
b a
1: 1 1
2: 2 1
3: 3 1
4: 4 1
5: 5 1
# However this has not assigned by reference within `DT`
DT
b
1: 1
2: 2
3: 3
4: 4
5: 5
If I use the more correct
tracmem(DT)
DT <- foo(DT)
# This works without copying
DT
b a
1: 1 1
2: 2 1
3: 3 1
4: 4 1
5: 5 1
untracemem(DT)
If I use eval
and substitute
within the function
fooCall(DT)
b a
1: 1 1
2: 2 1
3: 3 1
4: 4 1
5: 5 1
# it does assign by reference
DT
b a
1: 1 1
2: 2 1
3: 3 1
4: 4 1
5: 5 1
Should I stick with
DT <- foo(DT)
or the eval
/substitute
route, or
- Is there something I'm not understanding about how
data
loads datasets, even when not lazy?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…