Data.table: `[.data.table` requires import of other function to work when data.table is imported by package

Created on 8 Sep 2017  路  8Comments  路  Source: Rdatatable/data.table

When writing packages, I generally declare an Imports dependency in the DESCRIPTION then use fully qualified names when invoking the package functions, e.g. data.table::fread().

However, this method fails to import the S3 methods into a namespace that my package can use. Specifically, [ fails and falls back to the [.data.frame unless I explicitly import at least one function from the data.table package in my NAMESPACE. While I can add importFrom(data.table, ...), it seems a bit of a hack to me.

Is this a data.table specific issue, or a general failing in R that requires with regard to S3 methods in package namespaces?

Most helpful comment

You need to include that line in a .R file, not NAMESPACE. I usually put it in the file that has my package documentation, @importFrom statements etc. For example:

# Import package operators
#' @importFrom magrittr "%>%" "%<>%"
#' @importFrom data.table ":=" "%like%" "%between%"
#' @export
magrittr::`%>%`

# Make sure data.table knows we know we're using it
.datatable.aware = TRUE

# Prevent R CMD check from complaining about the use of pipe expressions
# standard data.table variables
if (getRversion() >= "2.15.1")
  utils::globalVariables(c(".", ".I", ".N", ".SD"), utils::packageName())

All 8 comments

Not sure if this addresses it, but the usual reference is the final question of the FAQ: https://stackoverflow.com/questions/10527072/using-data-table-package-inside-my-own-package/10529888#10529888

Possibly related to #2306?

The final FAQ covers it but is an ugly hack in my opinion. I should not have to pollute my package's namespace.

As I said in the original post, , importing a single symbol from the data.table package fixes the problem. I guess that's because the import() and importFrom() in the NAMESPACE generally do the same thing, with importFrom() having an additional final operation (filtering symbols).

For now I'll live with importFrom(data.table,data.table)

close #2341

One last remark: apparently the directions in the data.table::cedata comments (lines 17-21) also work. Adding

.datatable.aware = TRUE

to my package also fixes the issue. Should this be part of the FAQ?

One last remark: apparently the directions in the data.table::cedata comments (lines 17-21) also work. Adding

.datatable.aware = TRUE

to my package also fixes the issue. Should this be part of the FAQ?

Where and how exactly do you add this line? Adding this straightforwardly to NAMESPACE returns me

Error: unknown namespace directive: TRUE

Adding this anywhere else does not work, only the importFrom(data.table,data.table) worked for me.

@jangorecki is this part of the importing DT vignette?

You need to include that line in a .R file, not NAMESPACE. I usually put it in the file that has my package documentation, @importFrom statements etc. For example:

# Import package operators
#' @importFrom magrittr "%>%" "%<>%"
#' @importFrom data.table ":=" "%like%" "%between%"
#' @export
magrittr::`%>%`

# Make sure data.table knows we know we're using it
.datatable.aware = TRUE

# Prevent R CMD check from complaining about the use of pipe expressions
# standard data.table variables
if (getRversion() >= "2.15.1")
  utils::globalVariables(c(".", ".I", ".N", ".SD"), utils::packageName())

@MichaelChirico not, but could be.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jameslamb picture jameslamb  路  3Comments

st-pasha picture st-pasha  路  3Comments

alex46015 picture alex46015  路  3Comments

DavidArenburg picture DavidArenburg  路  3Comments

mattdowle picture mattdowle  路  3Comments