Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import all the functions of a package except one when building a package

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!

like image 785
Julien Massardier Avatar asked Aug 17 '18 16:08

Julien Massardier


People also ask

How do I import everything into a package?

So you will need to create a list of strings of everything in your package and then do a "from packageName import *" to import everything in this module so when you import this elsewhere, all those are also imported within this namespace.

How do you import all functions in Python?

To import all functions in a script, use * . This imports all the functions defined in script_in_cwd.py . If script_in_cwd.py has import statements, then * will also import those libraries, modules, and functions. For instance, if script_in_cwd.py had import numpy , then the above statement would also import numpy .

How do you import packages in R?

In RStudio go to Tools → Install Packages and in the Install from option select Repository (CRAN) and then specify the packages you want. In classic R IDE go to Packages → Install package(s) , select a mirror and install the package.


1 Answers

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.

like image 106
Thomas Avatar answered Sep 19 '22 08:09

Thomas