Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 206
Dhskjlkakdh Avatar asked Feb 15 '23 00:02

Dhskjlkakdh


1 Answers

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
like image 164
BrodieG Avatar answered Feb 17 '23 14:02

BrodieG