Using setDT inside a function

Great question! The warning message should say: … and fixed by taking a shallow copy of the whole table …. Will fix this.

setDT does two things:

  • set the class to data.table from data.frame/list
  • use alloc.col to over-allocate columns (so that := can be used directly)

And the 2nd step requires a shallow copy, if the input is not a data.table already. And this is why we assign the value back to the symbol in it’s environment (setDT’s parent frame). But the parent frame for setDT is your function f(). Therefore the setDT(df) within your function has gone through smoothly, but the df that resides in the global environment will only have it’s class changed, not the over-allocation (as the shallow copy severed the link).

And in the next step, := detects that and shallow copies once again to over-allocate.

The idea so far is to use setDT to convert to data.tables before providing it to a function. But I’d like that these cases be resolved (will take a look).

Thanks a bunch!

Leave a Comment