I am creating a package that works with data.table and which should be tested with a test package. Although the code works fine when invoked from the command line, I run into problems when invoked from a test case. It seems that the function [] from the base package, i.e. The function for data.frames is used when running tests.
I created a minimal example that can be found here: https://github.com/utalo/test_datatable_testthat
The package contains one function:
test <- function() { dt <- data.table(MESSAGE="Test 1234567890",TYPE="ERROR") dt[,.(MESSAGE=strwrap(MESSAGE,width = 10)),by=.(TYPE)] }
When calling test.datatable.testthat:::test() from the command line, I get the expected result:
TYPE MESSAGE 1: ERROR Test 2: ERROR 1234567890
However, when performing the following unit test:
test_that("Test package",{ dt <- test() expected_res <- structure(list(TYPE = c("ERROR", "ERROR"), MESSAGE = c("Test","1234567890")), row.names = c(NA, -2L), class = c("data.table","data.frame"), .Names = c("TYPE", "MESSAGE")) expect_equal(dt,expected_res) })
I get an error message:
1 1. Error: Test package ------------------------------------------------------------------------------------------------------- could not find function "." 1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls, message = function(c) invokeRestart("muffleMessage")) 2: eval(code, new_test_environment) 3: eval(expr, envir, enclos) 4: test() at test.R:4 5: dt[, .(MESSAGE = strwrap(MESSAGE, width = 10)), by = .(TYPE)] at test.datatable.testthat/R/hello.R:5 6: `[.data.table`(dt, , .(MESSAGE = strwrap(MESSAGE, width = 10)), by = .(TYPE)) at C:\Users\D057806\Documents\R\test.datatable.testthat/R/hello.R:5 7: `[.data.frame`(x, i, j)
As you can see, inside the test, [] data.frame is called. My first assumption was that the dependency on the data.table package was not declared correctly. This is my DESCRIPTION file:
Package: test.datatable.testthat Type: Package Title: What the Package Does (Title Case) Version: 0.1 Date: 2016-04-07 Authors@R : person("First", "Last", email = " first.last@example.com ", role = c("aut", "cre")) Description: More about what it does (maybe more than one line) License: What license is it under? LazyData: TRUE Depends: data.table Suggests: testthat RoxygenNote: 5.0.1
According to Using the data.table package inside my own package , this should be enough to declare data.table as a dependent package. However, this does not seem to be the case.
Any hints as to why my function works when called directly, but not in the context of testthat?