devtools::test when package Depends on data.table, but does not import it #192

Closed
gsee opened this Issue Nov 12, 2012 · 10 comments

Comments

Projects
None yet
7 participants

gsee commented Nov 12, 2012

When a package Depends on data.table (and does not import it), sometimes using devtools::test on the package can break some functions that use data.table. I filed a bug report for data.table, but it's not clear that is a data.table issue. Here is the link to the data.table bug report: https://r-forge.r-project.org/tracker/?func=detail&atid=975&aid=2367&group_id=240

I don't see a way to attach files to this, but here is a link (that should be a direct download) to the package that I attached to the data.table bug report: https://r-forge.r-project.org/tracker/download.php/240/975/2367/275/anRpackage_1.0.tar.gz

The package has 2 functions: myFun assigns some values to a column of a data.table by reference. myFun2() creates a data.table, and then calls myFun() to change the values of one of the columns of the data.table myFun2() created.

The package also has a testthat test that simply tests that myFun2 returns a data.table.

The package passes R CMD check, and test_dir. However, if I use devtools::test, I get an error, and for the remainder of the R session, myFun() and myFun2() will fail with the same error.

Here is a link to the output from an interactive session (attached to the data.table bug report) showing the problem: https://r-forge.r-project.org/tracker/download.php/240/975/2367/276/interactive.txt

Wild guess is that devtools needs to create .Depends in the namespace environment, to mimick more closely what R itself does. When I looked at devtools NEWS for 0.8, it appeared that devtools simulates a namespace, or creates one itself somehow. So it isn't a namespace exactly as would be created by R CMD INSTALL followed by library() ?

Owner

hadley commented Nov 12, 2012

It's not exactly the same, but it's getting closer each version. Since we use imports and not depends, the depends code hasn't been tested as well, but bug reports like this help us prioritise features for new versions.

Great, thanks. I'll close the data.table bug report then, now it's confirmed as likely a devtools issue.

gsee commented Nov 13, 2012

Thanks to a suggestion from Matthew Dowle, a workaround for me is to add .datatable.aware=TRUE in my package, which makes this problem disappear.

hadley added a commit that referenced this issue Dec 14, 2012

Owner

hadley commented Dec 14, 2012

Yeah, this was probably related to the fact we weren't loading any packages in depends.

hadley added a commit that referenced this issue Dec 14, 2012

Merge pull request #210 from hadley/depends
Require packages in Depends. Fixes #161, #178, #192.
Owner

hadley commented Dec 14, 2012

Closed in fa70de6

@hadley hadley closed this Dec 14, 2012

Contributor

dlebauer commented Jul 2, 2014

@hadley Is this bug related to http://stackoverflow.com/q/24501245/513006 and http://stackoverflow.com/q/23252231/513006? If so, should it be reopened?

Here is a reproducible example from the first link

library(devtools)
setwd(tempdir())
# make dummy package called foo
create("foo")
setwd("foo")

# add data.table as a package dependency
a <- readLines("DESCRIPTION")
depends.idx <- grepl("Depends", a)
a[depends.idx] <- paste0(a[depends.idx], ", data.table")
writeLines(a, "DESCRIPTION")

# create a dummy function 
writeLines("myfunction <- function() {a <- data.table(b=1); return(a[,b])}",
            "R/foo.R")

# check and throw error
check() # fails but documents and compiles
library(foo)
myfunction()
Contributor

krlmlr commented May 26, 2015

I can't replicate this issue with R 3.2.0, data.table 1.9.4 and devtools 1.8.0. Need to create a NAMESPACE file to get rid of a R CMD check warning; code for myfunction also had errors. Full script:

library(devtools)
setwd(tempdir())
# make dummy package called foo
create("foo")
setwd("foo")

# add data.table as a package dependency
a <- readLines("DESCRIPTION")
depends.idx <- grepl("Depends", a)
a[depends.idx] <- paste0(a[depends.idx], ", data.table")
writeLines(a, "DESCRIPTION")

writeLines("import(data.table)", "NAMESPACE")

# create a dummy function 
writeLines("myfunction <- function() {a <- data.table(b=1); return(a[,'b'])}",
            "R/foo.R")

# check and throw error
check() # fails but documents and compiles
library(foo)
myfunction()
sessionInfo()

utalo commented Apr 11, 2016

I seem to be encountering the same issue as discussed above. I posted a related question on stackoverflow: http://stackoverflow.com/questions/36547235/r-data-table-and-testthat-package
Reproducible code example can be found here: https://github.com/utalo/test_datatable_testthat
I am using R-3.2.3, data.table 1.9.6 and devtools 1.10.0.

Bohdan-Khomtchouk commented Apr 29, 2016

Also getting identical error:

Error in `[.data.frame`(x, i, j) : object 'my_object' not found
Called from: `[.data.frame`(x, i, j)

Tried all the tips included here, read all relevant Stack Overflow posts, and updated my devtools and data.table libraries to the most recent versions. No luck.

@jangorecki jangorecki referenced this issue in Rdatatable/data.table Nov 1, 2017

Open

testthat does not recognize data.tables ? #2053

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment