Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

call `[.data.table` explicitly

Tags:

r

data.table

I have a weird behavior, when running my R program on another machine. When I try to run a data.table join df1[df2] I get the error message

Error in `[.default`(x, i) : invalid subscript type 'list'

I assume that for some reason the R environment on the other machine does not find the data.table bracket function (Although I have loaded the library there).

To force R to use the bracket from data.table I would like to call the bracket function explicitly, but I can't find out how.

Here what I've tried

library(data.table)    
df1 <- data.frame(a = c("a1","a2","a3"), n = c(1,2,3), b = c(T,T,T))
df2 <- data.frame(a = c("a1","a2","a3"), n = c(1,2,3), b = c(F,T,F))

df1 <- data.table(df1)
df2 <- data.table(df2)
setkey(df1,a,n,b)
setkey(df2,a,n,b)

df1[df2] # produces `[.default`(x, i) : invalid subscript type 'list'

# my tries to call `[.data.table` explicitly all produce errors
`[.data.table`(df1, df2)
data.table::`[.data.table`(df1, df2)
data.table::`[`(df1, df2)

How can I use the bracket function from the data.table package explicitly?

EDIT:

OK, I'm trying to find the root cause of the error. I'm using R version 3.2.1,

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] data.table_1.9.2 mypackage_1.0    ROracle_1.1-10   DBI_0.2-7

loaded via a namespace (and not attached):
[1] plyr_1.8.1    reshape2_1.4  Rcpp_0.11.2   stringr_0.6.2

is.data.table gives TRUE on both, df1 and df2 just before calling df1[df2] (I'm debugging through the code).

The function that contains the codeline df1[df2] is called inside mypackage_1.0 (A package I'm developing). I have noticed, that if I run the code line by line, instead of calling my package function and debugging it, the code works as expected. So I assume there is something wrong with the package. In the DESCRIPTION file I only import the package data.table under "Suggests". Might it be related to that?

like image 785
Fabian Braun Avatar asked Apr 30 '26 08:04

Fabian Braun


1 Answers

To long for a comment so posting as answer.
General comments related to your case.

  1. You can call [.data.table explicitly by calling not exported data.table function using ::: operator.

data.table:::`[.data.table`(x, i)

Using ::: is not a best practice, as it makes you responsible for a function which package author decided not to expose to users directly. You should keep that in mind, still the R CMD check will not raise an error or warning. According to Writing R Extensions:

Using foo:::f instead of foo::f allows access to unexported objects. This is generally not recommended, as the semantics of unexported objects may be changed by the package author in routine maintenance.

In my opinion if you develop and internal package which will be deployed with explicitly stated version of dependencies, it is pretty safe to use :::.

  1. Update your data.table version, 1.9.2 is pretty old release already.
  2. In your DESCRIPTION file use Imports data.table and don't forget to define imports in NAMESPACE file
  3. Debug your problematic machine with the following

if(is.data.table(df1) && is.data.table(df2)) df1[df2] else stop("not a data.table")
  1. Use sessionInfo() as one of your first step in debugging cross package issues to track attached packagess.
like image 87
jangorecki Avatar answered May 01 '26 23:05

jangorecki