data.table错误通过knitr使用时,gWidgetsWWW [英] data.table error when used through knitr, gWidgetsWWW

查看:133
本文介绍了data.table错误通过knitr使用时,gWidgetsWWW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试gWidgetsWWW,遇到一个奇怪的错误。我创建了一个带有处理程序的按钮,用于编写一个使用data.table赋值运算符:=的报表。该报告返回了此错误:


错误::=定义仅用于j,即
DT [i,col:= 1L]和DT [,newcol:= sum(colB),by = colA]不是DT [i] $ col:= 1L而不是DT [,{newcol1:= 1L; newcol2:= 2L}]。
请参阅帮助(:=)。检查is.data.table(DT)为TRUE。


报表生成正常,使用knit2html直接并通过RStudio的Knit HTML 按钮,所以我不确定为什么当knit2html被处理程序调用失败。



这里是一个gWidgetsWWW窗口test_gui.R:

 库(gWidgetsWWW)
库(knitr)

w <-gwindow b $ b g <-ggroup(horizo​​ntal = F,cont = w)
b <-gbutton(Report Button,cont = g,handler = function(h,...){
knit2html (test_report.Rmd)
localServerOpen(test_report.html)
})

visible(w)<-T



下面是一个示例R Markdown Doc生成错误:

 测试报告
===========

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

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

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

```

不知道为什么,但是当我调用localServerOpen(test_gui.R ),然后点击按钮,我得到错误...



任何想法?

解决方案

这似乎是一个环境问题。这可能是 data.table gWidgetsWWW 之间的问题。在 knitr 的一侧,至少有一个解决方案,即指定 knitr 的环境为全局环境,例如

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

编辑



无关 knitr ,请尝试:

 库(gWidgetsWWW)

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

visible(w)<-TRUE
pre>

另存为 test_gui.R

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


b $ b

点击按钮,你也会看到错误。


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?

解决方案

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.

这篇关于data.table错误通过knitr使用时,gWidgetsWWW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆