I'm having a problem getting data.table to work in roxygen2 exported functions.
Here's a simple, fake function in a file called foo.R (located in the R directory of my package) which uses data.table:
#' Data.table test function
#' @export
foo <- function() {
m <- data.table(c1 = c(1,2,3))
print(is.data.table(m))
m[,sum(c1)]
}
If I copy and paste this function into R, this function works fine:
> foo <- function() {
+ m <- data.table(c1 = c(1,2,3))
+ print(is.data.table(m))
+ m[,sum(c1)]
+ }
> foo()
[1] TRUE
[1] 6
But if I simply load the exported function, R thinks that the data.table is a data.frame and breaks:
> rm(foo)
> load_all()
Loading test_package
> foo
function() {
m <- data.table(c1 = c(1,2,3))
print(is.data.table(m))
m[,sum(c1)]
}
<environment: namespace:test_package>
> foo()
[1] TRUE
Error in `[.data.frame`(x, i, j) : object 'c1' not found
What's up?
UPDATE
Thanks to @GSee for the help. Looks like this is actually a devtools issue. Check out the interactive command line code below.
After loading the test_package library, foo
runs correctly:
> foo
function ()
{
m <- data.table(c1 = c(1, 2, 3))
print(is.data.table(m))
m[, sum(c1)]
}
<environment: namespace:test_package>
> foo()
[1] TRUE
[1] 6
Running load_all()
breaks foo:
> load_all()
Loading test_package
> foo()
[1] TRUE
Error in `[.data.frame`(x, i, j) : object 'c1' not found
Somehow source('R/foo.R')
revives foo functionality:
> source('R/foo.R')
> foo
function() {
m <- data.table(c1 = c(1,2,3))
print(is.data.table(m))
m[,sum(c1)]
}
> foo()
[1] TRUE
[1] 6
And future calls to load_all()
don't break foo
again:
> load_all()
Loading test_package
> foo
function() {
m <- data.table(c1 = c(1,2,3))
print(is.data.table(m))
m[,sum(c1)]
}
> foo()
[1] TRUE
[1] 6
Also, I updated to devtools 1.5 and tried adding .datatable.aware=TRUE
, but that didn't seem to do anything.