Why would my non-interactive R session send the data.table as if it were a data.frame?

I have an object data.tableon which I would like to do a simple search:

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

In my interactive R session, this piece 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 script 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 sending dt[....to [. data.frame , not [. data.table . Any thoughts on why?

+4
source share
2 answers

Posterity: . import(data.table) NAMESPACE . , , , data.table , [.data.table data.table , .. data.table, , . , , .

EDIT: :
data.table

+3

, library(data.table), . -, data.table, batch exec. , b/c - data.table, , :

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
+5

Source: https://habr.com/ru/post/1523429/


All Articles