Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

r - data.table error when used through knitr, gWidgetsWWW

I'm experimenting with gWidgetsWWW and encountered a strange error. I created a button with a handler to knit2html a report which used the data.table assignment operator ":=". The report came back with this error:

Error: := is defined for use in j only, and (currently) only once; i.e., DT[i,col:=1L] and DT[,newcol:=sum(colB),by=colA] are ok, but not DT[i,col]:=1L, not DT[i]$col:=1L and not DT[,{newcol1:=1L;newcol2:=2L}]. Please see help(":="). Check is.data.table(DT) is TRUE.

The report generates as expected using knit2html directly and also through RStudio's "Knit HTML" button, so I'm unsure why it fails when knit2html is called by the handler.

Here is a gWidgetsWWW window "test_gui.R":

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 R Markdown Doc which produces the error:

Test Report
===========

```{r test_chunk}
library(data.table)

df<-data.frame(State=rownames(USArrests),USArrests)

data.table(df)[,State:=tolower(State)]

```

Not sure why, but when I call localServerOpen("test_gui.R") and click the button, I get the error...

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

The full assignInNamespace command is :

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

If you're not sure of the exact namespace name, set options(datatable.verbose=TRUE), run the offending line again and an output message should tell you which namespace name was decided not to be data.table aware.

Asof the time of this edit, the packages on data.table's whitelist (v1.9.3) are :

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

They tend to be packages which take user code as input and run that in their own environment.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...