Dhskjlkakdh
Dhskjlkakdh

Reputation: 12751

Why might my non-interactive R session be dispatching a data.table as if it were a data.frame?

I have a data.table object on which I'd like to do a simple lookup:

print(class(dt))
print(colnames(dt))
print(dt[region == "UK", ])

In my interactive R session, this chunk of code does exactly what it should.

[1] "data.table" "data.frame"
[1] "region"            "site"              "visit"            
[4] "connectionfailure" "dnserror"          "http404"          
# ... output ...

In a non-interactive scripted session, I get a confusing error:

[1] "data.table" "data.frame"
[1] "region"            "site"              "visit"            
[4] "connectionfailure" "dnserror"          "http404"          
Error in `[.data.frame`(x, i, j) : object 'region' not found

It looks like R is dispatching dt[.... to [.data.frame rather than to [.data.table. Any thoughts as to why?

Upvotes: 4

Views: 952

Answers (2)

Dhskjlkakdh
Dhskjlkakdh

Reputation: 12751

Posterity: in batch execution the problematic code is loaded from a custom package. I neglected to include import(data.table) in my package's NAMESPACE file. I could be wrong, but I think this would still have worked if data.table didn't include an explicit check that the [.data.table calling environment includes data.table in its namespace, i.e. data.table is perhaps overreaching. Still, I'm sure there must be a good reason for this check.

EDIT: More info about that explicit check here:
Using data.table package inside my own package

Upvotes: 3

BrodieG
BrodieG

Reputation: 52667

Most likely you don't have library(data.table) set up in your batch execution. Could be something based on your user profile auto-loading data.table, but not batch exec. Also, just b/c something has a class data.table, doesn't mean the package is loaded:

library(data.table)
dt <- data.table(a=1:3)
detach("package:data.table", unload=TRUE)
class(dt)
# [1] "data.table" "data.frame"
setkey(dt, a)
# Error: could not find function "setkey"
library(data.table)
setkey(dt, a)
#works

Upvotes: 5

Related Questions