When I run...
library(data.table)
DT = CJ(id = 1, id2 = 1:3)[, .(
d = as.IDate("2017-01-01")+1:10, v = id*10 + id2*1:10
), by=.(id, id2)]
plotDT = DT[, {
par(mfrow = c(uniqueN(id2), 1), mar = c(2,2,.5,2))
.SD[,{
plot(d, v, type="l")
}, by=id2]
.(p = .(recordPlot()))
}, by=id]
x11()
plotDT[id == 1L, replayPlot(p[[1]])]
The plot from DT[...]
is correct:
While the replay is wrong:
In the recordedplot, the final values of x
and y
are magically applied to all three graphs, probably because of how data.table handles pointers to columns defined in by=
groups.
I'm not really sure I understand what's going on, though, because I can change the example superficially, writing v = id2*1:10
instead of v = id*10 + id2*1:10
and -- poof -- the problem disappears.
I'd like to know (i) why is this happening and (ii) what simple tweak can I make to get around it?
One working hack is to write j
like
with(copy(.SD), {yada yada})
or similar, which ensures that references are to the per-group copy of .SD
and don't get conflated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With