Search code examples
rvisual-studiodata.tabler-markdownrtvs

RTVS: Unable to Knit Document with data.table


I am trying to get a simple R Markdown document working with the data.table package in Visual Studio (RTVS) 2017 (15.7.4), to no avail.

Here is a minimum reproducible .rmd file (with some optional debug options turned on):

---
title: "Untitled"
output: html_document
---

```{r knitr-setup, include = FALSE}
library(knitr)

knitr::opts_chunk$set(eval = TRUE)
opts_knit$set(progress = FALSE, verbose = TRUE)

```

```{r test_id, message=FALSE, results="show", echo=TRUE, warning=FALSE}

require(rmarkdown)
require(data.table, quietly = TRUE, warn.conflicts = FALSE)
options(datatable.verbose = TRUE)


DT = data.table(x=1:3, y=4:6)    # no
DT                               # yes
DT[, z := 7:9]                   # no
print(DT[, z := 10:12])          # yes
if (1 < 2) DT[, a := 1L]         # no
DT                               # yes
```

Some text.

```{r}
sessionInfo()
```

I have looked at similar issues like these which have the same symptoms:

data.table error when used through knitr, gWidgetsWWW

I have tried the namespace override similar to how I use it with "devtools" package development, doesn't seem to matter.

https://github.com/rstudio/rmarkdown/issues/278

As I thought it may be a problem with how I am referencing the package (or namespaces, etc, etc). However, the exact same file runs completely fine in "R Studio". So I am not sure that's the case.

The error I am getting is:

R Evaluation failed:

rtvs::rmarkdown_publish(blob_id = 29, output_format = "html_document", encoding = 'cp1252')

Error in ':='(z, 7:9): Check that is.data.table(DT) == TRUE. Otherwiese, := and ':=' are defined for us in j, once only and in particular ways. See help(":=").

Again, this same document has no problems at all in R Studio.

I did notice the two shells call pandoc slightly different:

R Studio call:

"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS dt_error.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+smart --output dt_error.html --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\bmore\Documents\R\win-library\3.5\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\bmore\AppData\Local\Temp\Rtmp0cb9Vo\rmarkdown-stra0bc15f917ea.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

Visual Studio Call:

"C:/PROGRA~2/Pandoc/pandoc" +RTS -K512m -RTS rmd_8c885bcf5786.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+smart --output pandoc8c8870d27b22.html --email-obfuscation none --self-contained --standalone --section-divs --template "C:\Users\bmore\Documents\R\win-library\3.5\rmarkdown\rmd\h\default.html" --no-highlight --variable highlightjs=1 --variable "theme:bootstrap" --include-in-header "C:\Users\bmore\AppData\Local\Temp\Rtmp429dUm\rmarkdown-str8c886f7837b1.html" --mathjax --variable "mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

I have also submitted a bug report to Visual Studio Developer community, however, I am not entirely convinced it can't be resolved without changes to the IDE.

Note: The above code runs in the IDE/Interactive mode fine, when attempting to 'knit' as any output type (html, pdf, doc), the error occurs.

sessionInfo()

R version 3.5.0 (2018-04-23) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale: 1 LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
LC_TIME=English_United States.1252

attached base packages: 1 stats graphics grDevices utils
datasets methods base

other attached packages: 1 rmarkdown_1.10 knitr_1.20
ggplot2_2.2.1 dplyr_0.7.6 data.table_1.11.4

loaded via a namespace (and not attached): 1 Rcpp_0.12.17
bindr_0.1.1 magrittr_1.5 rtvs_1.0.0.0 tidyselect_0.2.4 munsell_0.5.0 colorspace_1.3-2 R6_2.2.2 rlang_0.2.1
stringr_1.3.1 plyr_1.8.4 tools_3.5.0 grid_3.5.0
gtable_0.2.0 [15] htmltools_0.3.6 yaml_2.1.19
rprojroot_1.3-2 lazyeval_0.2.1 assertthat_0.2.0 digest_0.6.15
tibble_1.4.2 bindrcpp_0.2.2 purrr_0.2.5 evaluate_0.10.1 glue_1.2.0 labeling_0.3 stringi_1.1.7 compiler_3.5.0


Solution

  • This is confirmed as fixed in data.table_1.11.8 and later per @Hugh Ugh's comment above.

    However, if anyone is constrained to use a prior version of data.table for some reason with RTVS, the workaround is to add:

    assignInNamespace("cedta.pkgEvalsUserCode", c(data.table:::cedta.pkgEvalsUserCode, "rtvs"), "data.table")
    

    In a script block, like so:

    ```{r additional-libraries, echo=FALSE}
    
        library(data.table, quietly = TRUE, warn.conflicts = FALSE)
    
        assignInNamespace("cedta.pkgEvalsUserCode", c(data.table:::cedta.pkgEvalsUserCode, "rtvs"), "data.table")
    
        }
        ```