2
votes

I'm currently building a series of test for my package and wanted to build the following test to make sure a function works within data.table. The test is as follows:

# 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)})

Note that the function findHorrificValues() is defined as:

findHorrificValues <- function(data) {
    is.nan(data) | is.infinite(data)
}

When executed as a script, there is no issue with the code, however when performing the script as a test I get the following error:

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

I have made sure that data.table is both an import and a dependency in my package description. As the data.table function is being called it doesn't appear to be the source of the issue. Any idea what is causing the error? Could it be an issue with the combination of testthat and data.table (functions from both packages appear in the stacktrace)?

1
From the call stack it looks like your expression is redirected to [.data.frame and fails most likely because of that. I would try to write a plain test without testthat, to make it clear about the source of the issue. Comment tests-all.R and put your test into script, changing testthat+expect_true to stopifnot. You need to include library() to your pkg at start of test script.jangorecki
It would appear that the error is due to it being redirected to [.data.frame, however I can't see why it would be redirected in the first place. I've checked to see if the object is a data.table with some simple flow control in my code and re-wrote the test including the library() call and without testthat as suggested. I still hit the same error.Glen Moutrie
Give me few minutes I will try to reproduce :)jangorecki
I've just put a copy of an R project online. When I ran it on my machine I hit the error I described in the post. Hopefully you'll be able to reproduce it too. github.com/glenMoutrie/StackOverflowDemo/tree/master/…Glen Moutrie

1 Answers

1
votes

I've put your code into a package which has only one function findHorrificValues.
The glen package imports data.table.
Content of package provided below, as the linked repo will probably be removed soon (you can fork it and update the link here if you don't care to keep it in your repos).

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/00check.log

* 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

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