Data.table error when using via knitr, gWidgetsWWW

I am experimenting with gWidgetsWWW and found a strange error. I created a button with a handler for the knit2html report, which used the assignment operator data.table ": =". The message returned with this error:

The error :: = is defined for use only in j and (currently) only once; those. DT [i, col: = 1L] and DT [, newcol: = sum (colB), by = colA] are fine, but not DT [i, col]: = 1L, not DT [i] $ col: = 1L, not DT [, {newcol1: = 1L; newcol2: = 2L}]. See Help (": ="). Check is.data.table (DT) is set to TRUE.

The report generates, as expected, using knit2html directly, as well as through the Knit HTML RStudio button, so I'm not sure why it fails when the knit2html handler is called by the handler.

Here is the gWidgetsWWW "test_gui.R" window:

library(gWidgetsWWW) library(knitr) w<-gwindow("Test Window") g<-ggroup(horizontal=F,cont=w) b<-gbutton("Report Button",cont=g,handler=function(h,...){ knit2html("test_report.Rmd") localServerOpen("test_report.html") }) visible(w)<-T 

Here is an example of an R Markdown Doc that causes an error:

 Test Report =========== ```{r test_chunk} library(data.table) df<-data.frame(State=rownames(USArrests),USArrests) data.table(df)[,State:=tolower(State)] ``` 

I don’t know why, but when I call localServerOpen ("test_gui.R") and click the button, I get an error ...

Any ideas?

+11
r knitr data.table gwidgets
Oct 28
source share
2 answers

This seems to be an environmental issue. This is probably the problem between data.table and gWidgetsWWW . On the knitr side knitr there is at least one solution that should specify the environment for knitr as a global environment, for example.

 knit2html("test_report.Rmd", envir = globalenv()) 

Edit:

To illustrate this issue, it doesn't matter to knitr , try the following:

 library(gWidgetsWWW) w<-gwindow("Test Window") g<-ggroup(horizontal=F,cont=w) b<-gbutton("Report Button",cont=g,handler=function(h,...){ library(data.table) df<-data.frame(State=rownames(USArrests),USArrests) print(data.table(df)[,State:=tolower(State)]) }) visible(w)<-TRUE 

Save it as test_gui.R and

 library(gWidgetsWWW) localServerOpen('test_gui.R') 

Press the button and you will also see an error.

+4
Oct 28 '12 at 21:25
source share

Thanks to Zach and Yihui, this is now fixed in data.table v1.8.3 on R-Forge.

 o gWidgetsWWW wasn't known as data.table aware, even though it mimics executing code in .GlobalEnv, #2340. data.table is now gWidgetsWWW aware. Further packages can be added if required by changing a new variable data.table:::cedta.override by using assignInNamespace(). Thanks to Zach Waite and Yihui Xie for investigating and providing reproducible examples. 

Full assignInNamespace command:

 assignInNamespace("cedta.override", c(data.table:::cedta.override,"<nsname>"), "data.table") 

If you are not sure of the exact name of the namespace, set options(datatable.verbose=TRUE) , run the violation line again, and the output message should tell you which namespace name was not accepted in order to be accessible to data.table.

During this edit, packages in whitelist data.table (v1.9.3):

 > data.table:::cedta.override [1] "gWidgetsWWW" "statET" "FastRWeb" "slidify" "rmarkdown" 

They are typically packages that enter a user code as input and run them in their environment.

+6
Oct 30
source share



All Articles