Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Speculation:</h2> <p>Here is what appears to be going on.</p> <p><strong>knitr</strong> quite sensibly caches objects as as soon as they are created. It then updates their cached value whenever it detects that they have been altered. </p> <p><strong>data.table</strong>, though, bypasses R's normal copy-by-value assignment and replacement mechanisms, and uses a <code>:=</code> operator rather than a <code>=</code>, <code>&lt;&lt;-</code>, or <code>&lt;-</code>. As a result <strong>knitr</strong> isn't picking up the signals that <code>DT</code> has been changed by <code>DT[, c:=5]</code>.</p> <h2>Solution:</h2> <p>Just add this block to your code wherever you'd like the current value of <code>DT</code> to be re-cached. It won't cost you anything memory or time-wise (since nothing except a reference is copied by <code>DT &lt;- DT</code>) but it does effectively send a (fake) signal to <strong>knitr</strong> that <code>DT</code> has been updated:</p> <pre><code>```{r, cache=TRUE, echo=FALSE} DT &lt;- DT ``` </code></pre> <hr> <h2>Working version of example doc:</h2> <p>Check that it works by running this edited version of your doc:</p> <pre><code>```{r} library(data.table) ``` Data.Table Markdown ======================================================== Suppose we make a `data.table` in **R Markdown** ```{r, cache=TRUE} DT = data.table(a = rnorm(10)) ``` Then add a column using `:=` ```{r, cache=TRUE} DT[, c:=5] ``` ```{r, cache=TRUE, echo=FALSE} DT &lt;- DT ``` Then we display that in a non-cached block ```{r, cache=FALSE} DT ``` The first time you run this, the above will show a `c` column. The second, third, and nth times, it will as well. </code></pre>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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