假设我写了一个包“hello
”......
R>library(devtools)
R>install_github("jimhester/covr")
R>create("hello")
...使用单个导出函数:
hello <- function() {
dt<-as.data.table(list(a=1:10))
dt[,b:=a+2]
return(sum(dt[,b]))
}
为了使这个包工作,我还添加了
Depends:
data.table
到DESCRIPTION
文件。
现在我想为它编写一个单元测试:
R>use_testthat("testcovr")
...在文件tests/testthat/test-hello.R
中:
context("hello")
test_that("hello works", {
expect_equal(hello(), 75)
})
当我运行测试时,它们会失败:
R> test()
Loading hello
Loading required package: testthat
Testing hello
hello: 1
Failed -----------------------------------------------------------------------------------------------------------------------
1. Error: hello works (@test-hello.R#7) --------------------------------------------------------------------------------------
Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(":=").
1: expect_equal(hello(), 75) at /home/Adama-docs/Adam/linux/tmp/hello/tests/testthat/test-hello.R:7
2: compare(object, expected, ...)
3: hello()
4: dt[, `:=`(b, a + 2)] at /home/Adama-docs/Adam/linux/tmp/hello/R/hello.R:18
5: `[.data.table`(dt, , `:=`(b, a + 2)) at /home/Adama-docs/Adam/linux/tmp/hello/R/hello.R:18
6: `[.data.frame`(x, i, j)
7: `:=`(b, a + 2)
8: stop("Check that is.data.table(DT) == TRUE. Otherwise, := and `:=`(...) are defined for use in j, once only and in particular ways. See help(\":=\").")
DONE =========================================================================================================================
为什么?虽然我在testthat
和data.table
之间搜索了其他不兼容的情况,但它们与:=
运算符无关。他们责怪testthat
包裹。
这是我的会话信息:
Session info ------------------------------------------------------------------------------------------------------------------
setting value
version R version 3.3.0 (2016-05-03)
system x86_64, linux-gnu
ui RStudio (0.99.902)
language en_US
collate en_US.UTF-8
tz <NA>
date 2016-06-16
Packages ----------------------------------------------------------------------------------------------------------------------
package * version date source
chron 2.3-47 2015-06-24 CRAN (R 3.3.0)
crayon 1.3.1 2015-07-13 CRAN (R 3.3.0)
data.table * 1.9.6 2015-09-19 CRAN (R 3.3.0)
devtools * 1.11.1 2016-04-21 CRAN (R 3.3.0)
digest 0.6.9 2016-01-08 CRAN (R 3.2.4)
magrittr 1.5 2014-11-22 CRAN (R 3.2.0)
memoise 1.0.0 2016-01-29 CRAN (R 3.3.0)
R6 2.1.2 2016-01-26 CRAN (R 3.3.0)
Rcpp 0.12.5 2016-05-14 CRAN (R 3.3.0)
roxygen2 5.0.1 2015-11-11 CRAN (R 3.3.0)
stringi 1.1.1 2016-05-27 CRAN (R 3.3.0)
stringr 1.0.0 2015-04-30 CRAN (R 3.2.0)
testdt * 0.1.0 <NA> local
testthat * 1.0.2 2016-04-23 CRAN (R 3.3.0)
withr 1.0.1.9000 2016-06-15 Github (jimhester/withr@bd42181)
有什么解决方法吗?放弃data.table
对我来说不是一个选项,因为我的库处理的是非常大的数据集。