Use `[` method from data.table package in package development
Asked Answered
O

2

5

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!

Odontology answered 28/5, 2021 at 8:28 Comment(0)
H
5

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

Hola answered 28/5, 2021 at 9:58 Comment(1)
Thank you, this helped out a lot! I first struggled, since I added .datatable.aware=TRUE within the function body, which did not resolve my problem. After tinkering around with the working example you've provided, I noticed that you placed it outside the function body. Moving it there solved it! Thank you for taking time to help me out and providing the working example.Odontology
C
1

I think you don't need to import S3 method or to use :: like we do on functions.

In my opinion you just need to add data.table as a dependency in DESCRIPTION and it should be working.

R will know that you are applying [ to a data.table object and will use the correct method.

Culbertson answered 28/5, 2021 at 9:52 Comment(1)
Thank you for your reply! data.table was listed at the Imports: section of the DESCRIPTION file, yet this did not resolve the issue. Importing data.table in the NAMESPACE using import(data.table) worked (perhaps, because like you mentioned, R now correctly identifies the data.table object). However, since we elaborately use lubridate functions and import this library entirely, it resulted in some warnings due to similarly named functions between data.table and lubridate. Therefor, we wanted to omit the import of data.table in the NAMESPACE.Odontology

© 2022 - 2024 — McMap. All rights reserved.