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 · 9 comments

Comments

@sz-cgt
Copy link

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

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

Possibly related to #2306?

@sz-cgt
Copy link
Author

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

@jangorecki is this part of the importing DT vignette?

@sz-cgt
Copy link
Author

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

@MichaelChirico not, but could be.

@jangorecki
Copy link
Member

If needed in future, a repex

f = function() data.table::as.data.table(iris)
g = function(x) x[1]
package.skeleton("pkg", list=c("f", "g"))
rm(f, g)
cat("Imports: data.table\n", file="pkg/DESCRIPTION", append=TRUE)
unlink("pkg/man", recursive=TRUE)
system("R CMD build pkg")
system("R CMD INSTALL pkg_1.0.tar.gz")

library(pkg)
x = f()
options(datatable.verbose=TRUE)
g(x)
#cedta decided 'pkg' wasn't data.table aware. Here is call stack with [[1L]] applied:
#[[1]]
#g
#
#[[2]]
#`[`
#
#[[3]]
#`[.data.table`
#
#[[4]]
#cedta
#
#     Sepal.Length
#            <num>
#  1:          5.1
#  2:          4.9
#  3:          4.7
#  4:          4.6
#  5:          5.0
# ---             
#146:          6.7
#147:          6.3
#148:          6.5
#149:          6.2
#150:          5.9

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