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

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

问题描述

我正在试验 gWidgetsWWW 并遇到了一个奇怪的错误.我创建了一个带有处理程序的按钮来 knit2html 使用 data.table 赋值运算符:="的报告.报告返回此错误:

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:

错误::= 被定义为仅在 j 中使用,并且(当前)仅使用一次;IE.,DT[i,col:=1L] 和 DT[,newcol:=sum(colB),by=colA] 可以,但不行DT[i,col]:=1L,不是 DT[i]$col:=1L,也不是 DT[,{newcol1:=1L;newcol2:=2L}].请参阅帮助(:=").检查 is.data.table(DT) 是否为 TRUE.

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.

该报告直接使用 knit2html 以及通过 RStudio 的Knit HTML"按钮按预期生成,所以我不确定为什么处理程序调用 knit2html 时它会失败.

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.

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

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

这是一个产生错误的示例 R Markdown Doc:

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

```

不知道为什么,但是当我调用 localServerOpen("test_gui.R") 并单击按钮时,我得到了错误...

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

有什么想法吗?

推荐答案

这似乎是环境问题.这可能是 data.tablegWidgetsWWW 之间的问题.在knitr方面,至少有一个解决方案,就是将knitr的环境指定为全局环境,例如

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

为了说明这个问题与 knitr 无关,试试这个:

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

另存为test_gui.R,然后

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

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

Click the button and you will also see the error.

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

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