Using the distill package and Rmarkdown for writing a blog, when rendering the .Rmd file I get errors when code chunks include data.table :=
and .
, but not functions such as data.table()
. The errors occur when the YAML header states draft: false
but not when draft: true
.
The R code chunk in the .Rmd file:
# create a data.table
library(data.table)
DT <- data.table(p = 1:5, q = 6:10)
# operate with ":="
DT[, r := p + q][]
# operate with "."
DT[, .(p)]
With draft: true
in the YAML header, the .Rmd file knits with no problem.
With draft: false
, I can still run the R code chunks with no error but knitting the .Rmd file produces this error:
Error: Check that is.data.table(DT) == TRUE. Otherwise, :=, `:=`(...)
and let(...) are defined for use in j, once only and in particular
ways. See help(":=").
Execution halted
If I comment out the line with the :=
operation, knitting the document produces a similar error on the .
operation:
Error in .(p) : could not find function "."
Calls: <Anonymous> ... withVisible -> eval -> eval -> [ ->
[.data.table -> [.data.frame
Execution halted
On Stackoverflow I found a similar issue 3 years ago with the rtvs package (RTVS: Unable to Knit Document with data.table) that suggested a workaround using assignInNamespace()
. I copied the suggestion to my code chunk and changed “rtvs” to “distill”, as follows:
# workaround
assignInNamespace("cedta.pkgEvalsUserCode",
c(data.table:::cedta.pkgEvalsUserCode, "distill"),
"data.table")
The help page for assignInNamespace()
says the function is intended for use within a package, which my blog is not, but it does solve my problem. Adding this workaround to the first code chunk eliminates the errors and the .Rmd file renders correctly.
My questions are:
- Does using
assignInNamespace()
in this way produce any problematic side effects? - Is there another solution or is this possibly a case where data.table or distill might need a patch?
Session information
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
other attached packages:
[1] data.table_1.14.3
loaded via a namespace (and not attached):
[1] fansi_1.0.2 digest_0.6.29 R6_2.5.1 jsonlite_1.7.3
[5] magrittr_2.0.2 evaluate_0.14 stringi_1.7.6 rlang_1.0.1
[9] cachem_1.0.6 cli_3.2.0 rstudioapi_0.13 jquerylib_0.1.4
[13] bslib_0.3.1 vctrs_0.3.8 rmarkdown_2.11 distill_1.3
[17] tools_4.1.2 stringr_1.4.0 xfun_0.29 yaml_2.2.2
[21] fastmap_1.1.0 compiler_4.1.2 memoise_2.0.1 htmltools_0.5.2
[25] knitr_1.37 downlit_0.4.0 sass_0.4.0
2022-06-06 Update. I've switched blog-authoring software from distill to quarto. The problem described above does not occur with quarto and porting older posts to quarto has been fairly straightforward.