Note that there are some explanatory texts on larger screens.

plurals
  1. PO`:=' does not work when used from a function on a data.table loaded from disk
    primarykey
    data
    text
    <p>The <code>:=</code> does not seem to work when called from a function on a data.table which has just been loaded from disk. Doing something with the <code>data.table</code> first does help. Is this a bug in <code>data.table</code>, or am I missing something (i.e. abusing data.table and <code>:=</code>)?</p> <p>Sample code:</p> <pre><code># Generate a dummy data.table and save to disk library(data.table) DT &lt;- data.table(a=c(1:10), b=c(1:10)) save(DT, file="c:/temp/DT.rData") # Now define and use a function which is supposed to change the data.table &gt; library(data.table) &gt; &gt; myfn &lt;- function(data="DT") { + DD &lt;- get(x=data, inherits=TRUE, mode="list") + DD[, c:="a", verbose=TRUE] } &gt; load(file="c:/temp/DT.rData") &gt; myfn(data="DT") .internal.selfref ptr is NULL. This is expected and normal for a data.table loaded from disk. If not, please report to datatable-help. Growing vector of column pointers from truelength 0 to 102 . A shallow copy has been taken, see ?alloc.col. Only a potential issue if two variables point to the same data (we can't yet detect that well) and if not you can safely ignore this. To avoid this message you could alloc.col() first, deep copy first using copy(), wrap with suppressWarnings() or increase the 'datatable.alloccol' option. .internal.selfref ptr is NULL. This is expected and normal for a data.table loaded from disk. If not, please report to datatable-help. Detected that j uses these columns: &lt;none&gt; Assigning to all 10 rows a b c 1: 1 1 a 2: 2 2 a 3: 3 3 a 4: 4 4 a 5: 5 5 a 6: 6 6 a 7: 7 7 a 8: 8 8 a 9: 9 9 a 10: 10 10 a # This seems to be the correct (desired) result, but does not "stick": &gt; DT a b 1: 1 1 2: 2 2 3: 3 3 4: 4 4 5: 5 5 6: 6 6 7: 7 7 8: 8 8 9: 9 9 10: 10 10 </code></pre> <p>Running the same code with an extra line <code>DT &lt;- copy(DT)</code> does change the DT as intended:</p> <pre><code>&gt; myfn &lt;- function(data="DT") { + DD &lt;- get(x=data, inherits=TRUE, mode="list") + DD[, c:="a"] } &gt; &gt; load(file="c:/temp/DT.rData") &gt; DT &lt;- copy(DT) &gt; myfn(data="DT") a b c 1: 1 1 a 2: 2 2 a 3: 3 3 a 4: 4 4 a 5: 5 5 a 6: 6 6 a 7: 7 7 a 8: 8 8 a 9: 9 9 a 10: 10 10 a &gt; DT a b c 1: 1 1 a 2: 2 2 a 3: 3 3 a 4: 4 4 a 5: 5 5 a 6: 6 6 a 7: 7 7 a 8: 8 8 a 9: 9 9 a 10: 10 10 a </code></pre> <p>Is there some fundamental problem in the way I am using <code>:=</code> above? I tried an alternative definition of <code>myfn()</code>, using <code>set()</code> but the result was the same. Thanks.</p> <pre><code>&gt; sessionInfo() R version 3.0.1 (2013-05-16) Platform: x86_64-w64-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] data.table_1.8.9 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload