data.table error when used through knitr, gWidgetsWWW

自作多情 提交于 2019-11-29 01:58:01

This seems to be an environment issue. That is probably a problem between data.table and gWidgetsWWW. On knitr's side, there is at least one solution, which is to specify the environment for knitr to be the global environment, e.g.

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

Edit:

To illustrate this issue is irrelevant to knitr, try this:

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')

Click the button and you will also see the error.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!