Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving plots in a data.table list column

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?

like image 571
Frank Avatar asked Feb 24 '17 22:02

Frank


1 Answers

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.

like image 133
Frank Avatar answered Oct 04 '22 16:10

Frank