11

I'm building an R package (mypackage) that imports data.table and another package (let's call it myotherpackage).

Imports: data.table, myotherpackage is in the DESCRIPTION file of mypackage.

myotherpackage imports dplyr, which has several functions named like the data.table functions, so I get warnings like this everytime I load mypackage:

Warning: replacing previous import ‘data.table::first’ by ‘dplyr::first’ when loading ‘mypackage’

Is there a way to import all the functions of data.table except "first" for example? I'd then use data.table::first in the code if I need to use it. Or is there a better way to handle it? I'm trying to avoid the warning every time someones imports the package. Thank you!

Julien Massardier
  • 1,326
  • 1
  • 11
  • 29
  • Use `importFrom(my other package, function)` directives in your NAMESPACE rather than importing all functions from the package. – Thomas Aug 18 '18 at 03:01
  • Thanks @Thomas. I was hoping that there would be an alternative to specifying every data.table function I want to import (which is pretty much all of them). Looks like I'm out of luck. – Julien Massardier Aug 18 '18 at 13:09
  • 2
    You can also specify `import(data.table, except=c(foo))`. See https://cran.r-project.org/doc/manuals/r-devel/R-exts.html#Specifying-imports-and-exports. I don't know if you can get that using roxygen markup, though. – Thomas Aug 18 '18 at 13:59
  • 4
    Yes! @Thomas, it worked! I had to use the following syntax in the function description (instead of `#' @import data.table`): `#' @rawNamespace import(data.table, except = first)` Do you wanna answer the question so I can give you credit? – Julien Massardier Aug 18 '18 at 21:54

1 Answers1

29

The NAMESPACE file is somewhat flexible here, as described in Writing R Extensions.

The two main import directives are:

import(PACKAGE)

which imports all objects in the namespace into your package. The second option is to do specific imports using:

importFrom(PACKAGE, foo)

which gives you access to foo() without needing the fully qualified reference PACKAGE::foo().

But these aren't the only two options. You can also use the except argument to exclude just a handful of imports:

import(PACKAGE, except=c(foo,bar))

which gives you everything from PACKAGE's namespace but foo() and bar(). This is useful - as in your case - for avoiding conflicts.

For roxygen, great catch on figuring out that you can do:

#' @rawNamespace import(PACKAGE, except = foo)

to pass a raw NAMESPACE directive through roxygen.

Thomas
  • 43,637
  • 12
  • 109
  • 140