Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[.data.table requires import of other function to work when data.table is imported by package #2341

Closed
sz-cgt opened this issue Sep 7, 2017 · 8 comments

Comments

@sz-cgt
Copy link

@sz-cgt sz-cgt commented Sep 7, 2017

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?

@franknarf1
Copy link
Contributor

@franknarf1 franknarf1 commented Sep 8, 2017

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

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Sep 8, 2017

Possibly related to #2306?

@sz-cgt
Copy link
Author

@sz-cgt sz-cgt commented Sep 8, 2017

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

@sz-cgt sz-cgt closed this as completed Sep 8, 2017
@sz-cgt
Copy link
Author

@sz-cgt sz-cgt commented Sep 8, 2017

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?

@nutle
Copy link

@nutle nutle commented Sep 21, 2018

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.

@MichaelChirico
Copy link
Member

@MichaelChirico MichaelChirico commented Sep 21, 2018

@jangorecki is this part of the importing DT vignette?

@sz-cgt
Copy link
Author

@sz-cgt sz-cgt commented Sep 21, 2018

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())

@jangorecki
Copy link
Member

@jangorecki jangorecki commented Sep 22, 2018

@MichaelChirico not, but could be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants