2013-04-23 16 views
7

data.table是一个精彩包,其中,唉,从checkUsage产生无理警告(代码来自herehere):data.table不与checkUsage发挥出色

> library(compiler) 
> compiler::enableJIT(3) 
> dt <- data.table(a = c(rep(3, 5), rep(4, 5)), b=1:10, c=11:20, d=21:30, key="a") 
> my.func <- function (dt) { 
    dt.out <- dt[, lapply(.SD, sum), by = a] 
    dt.out[, count := dt[, .N, by=a]$N] 
    dt.out 
} 
> checkUsage(my.func) 
<anonymous>: no visible binding for global variable ‘.SD’ (:2) 
<anonymous>: no visible binding for global variable ‘a’ (:2) 
<anonymous>: no visible binding for global variable ‘count’ (:3) 
<anonymous>: no visible binding for global variable ‘.N’ (:3) 
<anonymous>: no visible binding for global variable ‘a’ (:3) 
> my.func(dt) 
Note: no visible binding for global variable '.SD' 
Note: no visible binding for global variable 'a' 
Note: no visible binding for global variable 'count' 
Note: no visible binding for global variable '.N' 
Note: no visible binding for global variable 'a' 
    a b c d count 
1: 3 15 65 115  5 
2: 4 40 90 140  5 

a该警告可以是通过用by="a"代替by=a而避免,但我如何处理其他3个警告?

这对我很重要,因为这些警告混乱了屏幕并掩盖了合法的警告。由于警告是在my.func调用(启用JIT编译器时)上发出的,而不是仅由checkUsage发出,所以我倾向于将其称为bug

+0

查询:那些是my.func内的对象,那么为什么它们应该被认为是'global'变量呢? – 2013-04-23 14:53:58

+3

请参见[this](http://stackoverflow.com/a/15411032/967840)和[this](http://stackoverflow.com/a/8096882/967840) – GSee 2013-04-23 14:57:52

+1

我不知道'checkUsage'。如果有什么我可以在'data.table'中修改的,请告诉我。或者,也许有一个选择'checkUsage'。 – 2013-04-23 15:12:58

回答

4

UPDATE:现已在v1.8.11中解决。从NEWS

.SD.N.I.GRP.BY现在出口(如NULL)。所以这些NOTE不是由R CMD checkcodetools::checkUsage通过compiler::enableJIT()为他们生产的。被认为是utils::globalVariables(),但选择了出口。 感谢Sam Steingold提升,#2723

,并解决了列名标志counta的笔记,他们都可以用引号(甚至对:=的LHS)包裹。使用新的R会话(因为音符只是第一次)以下现在不产生音符。

$ R 
R version 3.0.1 (2013-05-16) -- "Good Sport" 
Copyright (C) 2013 The R Foundation for Statistical Computing 
Platform: x86_64-pc-linux-gnu (64-bit) 
> require(data.table) 
Loading required package: data.table 
data.table 1.8.11 For help type: help("data.table") 
> library(compiler) 
> compiler::enableJIT(3) 
[1] 0 
> dt <- data.table(a=c(rep(3,5),rep(4,5)), b=1:10, c=11:20, d=21:30, key="a") 
> my.func <- function (dt) { 
    dt.out <- dt[, lapply(.SD, sum), by = "a"] 
    dt.out[, "count" := dt[, .N, by="a"]$N] 
    dt.out 
} 
> my.func(dt) 
    a b c d count 
1: 3 15 65 115  5 
2: 4 40 90 140  5 
> checkUsage(my.func) 
> 
2

看来,在这个时候唯一的办法就是

my.func <- function (dt) { 
    .SD <- .N <- count <- a <- NULL # avoid inappropriate warnings 
    dt.out <- dt[, lapply(.SD, sum), by = a] 
    dt.out[, count := dt[, .N, by=a]$N] 
    dt.out 
} 

即本地绑定报告为未绑定的全局变量。

感谢@GSee的链接。