R 使用data.table和testthat时出错
我目前正在为我的包构建一系列测试,并希望构建以下测试,以确保函数在data.table中工作。测试如下:R 使用data.table和testthat时出错,r,data.table,r-package,testthat,R,Data.table,R Package,Testthat,我目前正在为我的包构建一系列测试,并希望构建以下测试,以确保函数在data.table中工作。测试如下: # Load libraries library(data.table) library(testthat) # Creating a test case test.case <- data.table(V1 = c(1,2,3,NaN,5,Inf,7,8,9,-Inf), V2 = 1:10,
# Load libraries
library(data.table)
library(testthat)
# Creating a test case
test.case <- data.table(V1 = c(1,2,3,NaN,5,Inf,7,8,9,-Inf),
V2 = 1:10,
V3 = c(NA,2:4,NaN,6:9,Inf))
# Source of the error
test.case <- test.case[,lapply(.SD,findHorrificValues)]
# Creating a comparison
comparison <- data.table(V1 = c(F,F,F,T,F,T,F,F,F,T),
V2 = rep(F,10),
V3 = rep(c(F,F,F,F,T),2))
value <- identical(test.case,comparison)
# Perform test
test_that("Compare two data.table objects",{expect_true(value)})
当作为脚本执行时,代码没有问题,但是当作为测试执行脚本时,我得到以下错误:
> devtools::test(fresh = T)
Loading packageName
Testing packageName
.
Error in .subset(x, j) : invalid subscript type 'list'
> traceback()
12: `[.data.frame`(x, i, j)
11: `[.data.table`(test.case, , lapply(.SD, findHorrificValues)) at test_fail.R#14
10: test.case[, lapply(.SD, findHorrificValues)] at test_fail.R#14
9: eval(expr, envir, enclos)
8: eval(exprs, envir)
7: sys.source2(fname, new.env(parent = env))
6: FUN(X[[i]], ...)
5: lapply(paths, test_file, env = env, reporter = current_reporter,
start_end_reporter = FALSE)
4: testthat::test_dir(test_path, filter = filter, env = env, ...)
3: force(code)
2: with_envvar(r_env_vars(), testthat::test_dir(test_path, filter = filter,
env = env, ...))
1: devtools::test(fresh = T)
我已经确保data.table在我的包描述中既是一个导入项又是一个依赖项。在调用data.table函数时,它似乎不是问题的根源。知道是什么导致了这个错误吗?testthat和data.table(两个包中的函数都出现在stacktrace中)的组合是否会出现问题?我已将您的代码放入一个只有一个函数的包中
FindCorrectificValues
该包导入数据表。
下面提供的包的内容,因为链接的回购协议可能很快就会被删除(如果您不想将其保留在回购协议中,您可以在此分叉并更新链接)
tests/tests.R
library(data.table)
library(glen)
# Creating a test case
test.case <- data.table(V1 = c(1,2,3,NaN,5,Inf,7,8,9,-Inf),
V2 = 1:10,
V3 = c(NA,2:4,NaN,6:9,Inf))
# Source of the error
test.case <- test.case[,lapply(.SD,findHorrificValues)]
# Creating a comparison
comparison <- data.table(V1 = c(F,F,F,T,F,T,F,F,F,T),
V2 = rep(F,10),
V3 = rep(c(F,F,F,F,T),2))
value <- identical(test.case,comparison)
stopifnot(value)
print(value)
glen.Rcheck/tests/tests.Rout
R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(data.table)
> library(glen)
>
> # Creating a test case
> test.case <- data.table(V1 = c(1,2,3,NaN,5,Inf,7,8,9,-Inf),
+ V2 = 1:10,
+ V3 = c(NA,2:4,NaN,6:9,Inf))
>
> # Source of the error
> test.case <- test.case[,lapply(.SD,findHorrificValues)]
>
> # Creating a comparison
> comparison <- data.table(V1 = c(F,F,F,T,F,T,F,F,F,T),
+ V2 = rep(F,10),
+ V3 = rep(c(F,F,F,F,T),2))
>
> value <- identical(test.case,comparison)
>
> stopifnot(value)
>
> print(value)
[1] TRUE
>
> proc.time()
user system elapsed
0.212 0.004 0.212
R版本3.2.3(2015-12-10)-“木制圣诞树”
版权所有(C)2015统计计算基础
平台:x86_64-pc-linux-gnu(64位)
R是免费软件,绝对没有保修。
在某些条件下,欢迎您重新分发。
键入“license()”或“license()”以获取分发详细信息。
R是一个有许多贡献者的协作项目。
键入“contributors()”以获取详细信息和
“引文()”介绍如何在出版物中引用R或R软件包。
对于某些演示,键入“demo()”;对于联机帮助,键入“help()”;或者
“help.start()”用于HTML浏览器界面的帮助。
键入“q()”退出R。
>库(数据表)
>图书馆(格伦)
>
>#创建测试用例
>测试用例
>#错误来源
>测试用例
>#创建比较
>比较
>价值观
>stopifnot(值)
>
>打印(值)
[1] 真的
>
>过程时间()
用户系统运行时间
0.212 0.004 0.212
从调用堆栈中,您的表达式似乎被重定向到[.data.frame
,很可能因此而失败。我会尝试编写一个没有testthat
的简单测试,以明确问题的根源。注释测试所有.R
并将测试放入脚本,将testthat+expect\u true
更改为stopifnot
。您需要包括库()
在测试脚本开始时发送到您的pkg。错误似乎是由于它被重定向到[.data.frame
,但是我不明白为什么首先会重定向它。我检查了对象是否是data.table
,代码中有一些简单的流控制,并重新编写了测试,包括库()
调用而不按建议执行测试。我仍然遇到了相同的错误。给我几分钟时间,我将尝试复制:)我刚刚将一个R项目的副本放到了网上。当我在我的机器上运行它时,我遇到了我在帖子中描述的错误。希望你也能够复制它。+1还给出了@GlenMoutrie,只是为了检查它是否被删除也正是如此。谢谢@MattDowle。我遇到了问题,因为我的函数声明中没有使用@import data.table
。@GlenMoutrie感谢更新。我不知道@import
。我想这与R上的RStudio/roxygen层有关。不管怎样,很高兴听到解决了这个问题。
* using log directory ‘/home/jan/Projects/glen.Rcheck’
* using R version 3.2.3 (2015-12-10)
* using platform: x86_64-pc-linux-gnu (64-bit)
* using session charset: UTF-8
* using options ‘--no-manual --no-build-vignettes --as-cran’
* checking for file ‘glen/DESCRIPTION’ ... OK
* checking extension type ... Package
* this is package ‘glen’ version ‘0.1.0’
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking for sufficient/correct file permissions ... OK
* checking whether package ‘glen’ can be installed ... OK
* checking installed package size ... OK
* checking package directory ... OK
* checking DESCRIPTION meta-information ... NOTE
Malformed Description field: should contain one or more complete sentences.
Non-standard license specification:
What license is it under?
Standardizable: FALSE
* checking top-level files ... OK
* checking for left-over files ... OK
* checking index information ... OK
* checking package subdirectories ... OK
* checking R files for non-ASCII characters ... OK
* checking R files for syntax errors ... OK
* checking whether the package can be loaded ... OK
* checking whether the package can be loaded with stated dependencies ... OK
* checking whether the package can be unloaded cleanly ... OK
* checking whether the namespace can be loaded with stated dependencies ... OK
* checking whether the namespace can be unloaded cleanly ... OK
* checking loading without being on the library search path ... OK
* checking dependencies in R code ... OK
* checking S3 generic/method consistency ... OK
* checking replacement functions ... OK
* checking foreign function calls ... OK
* checking R code for possible problems ... OK
* checking Rd files ... OK
* checking Rd metadata ... OK
* checking Rd line widths ... OK
* checking Rd cross-references ... OK
* checking for missing documentation entries ... OK
* checking for code/documentation mismatches ... OK
* checking Rd \usage sections ... OK
* checking Rd contents ... OK
* checking for unstated dependencies in examples ... OK
* checking examples ... NONE
* checking for unstated dependencies in ‘tests’ ... OK
* checking tests ... OK
Running ‘tests.R’
* DONE
Status: 1 NOTE
R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Copyright (C) 2015 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(data.table)
> library(glen)
>
> # Creating a test case
> test.case <- data.table(V1 = c(1,2,3,NaN,5,Inf,7,8,9,-Inf),
+ V2 = 1:10,
+ V3 = c(NA,2:4,NaN,6:9,Inf))
>
> # Source of the error
> test.case <- test.case[,lapply(.SD,findHorrificValues)]
>
> # Creating a comparison
> comparison <- data.table(V1 = c(F,F,F,T,F,T,F,F,F,T),
+ V2 = rep(F,10),
+ V3 = rep(c(F,F,F,F,T),2))
>
> value <- identical(test.case,comparison)
>
> stopifnot(value)
>
> print(value)
[1] TRUE
>
> proc.time()
user system elapsed
0.212 0.004 0.212