why does knitr caching fail for data.table `:=`?

前端 未结 2 1315
我在风中等你
我在风中等你 2021-02-04 02:53

This is related in spirit to this question, but must be different in mechanism.

If you try to cache a knitr chunk that contains a data.table

2条回答
  •  梦谈多话
    2021-02-04 03:47

    As indicated in the fourth comment under the answer by Josh O'Brien, I have added a new chunk option cache.vars to handle this very special case. In the second cached chunk, we can specify cache.vars='DT' so that knitr will save a copy of DT.

    ```{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, cache.vars='DT'}
    DT[, c:=5] 
    ```
    Then we display that in a non-cached block
    ```{r, cache=FALSE}
    DT
    ```
    

    The output is like this no matter how many times you compile the document:

    knitr works with data.table now

提交回复
热议问题