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。