Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use `[` method from data.table package in package development

We are creating a package where one of our functions uses functions of the data.table package. Instead of importing entire packages through our roxygen header, we try to use :: as much as possible in our code.

For a function, this is easy. For example:

data.table::setkey(our_data_1, our_variable)

Yet, we do not know how to do this for a method. For example:

our_data_3 <- our_data_1[our_data_2, roll = "nearest"]

where [ has a specific method for data.tables, which is indicated by:

methods(`[`)

I have tried multiple approaches. Multiple combinations, using @importFrom, failed. For example, adding the following line to our roxygen header...

@importFrom data.table `[.data.table`

...returned the following when running devtools::document():

Warning message:
object ‘[.data.table’ is not exported by 'namespace:data.table' 

I have also tried things like [.data.table within our code, but those failed as well...

Importing the entire data.table package in our roxygen header worked (@import data.table), but this is not preferred since we want to refer to the package of each function within our code (or at least use @importFrom).

Is there a way to use the [ method of data.table within the code of a function without importing the entire data.table package? Or is it at least possible to only import the method, for example through using @importFrom in our roxygen header?

Thank you in advance!

like image 378
Jonas B. Avatar asked May 28 '21 08:05

Jonas B.


Video Answer


1 Answers

There is no need to import S3 methods, they are automatically dispatched by class of an object.

In case of [ data.table method, there is a trick which we use to ensure that data.table passed to a library that expects data.frame, will be handled properly, as a data.frame. This handling is decided based on NAMESPACE file. If you don't import data.table in NAMESPACE then data.table method assumes you want to use it as data.frame.

You can state your intent explicitly by using extra variable .datatable.aware=TRUE in any of you R script files.

You should read Importing data.table vignette where this is well described.

I also put example package which you can run and debug from there if for some reason your code will still not work: https://gitlab.com/jangorecki/useDTmethod

like image 80
jangorecki Avatar answered Sep 29 '22 13:09

jangorecki