I am trying to build an R package that depends on the following packages: heatmaply, stats, and igraph. I've created a DESCRIPTION file that includes the following:
Imports:
heatmaply,
stats,
igraph
However, when I try to build, I'm getting the following warnings ("myPkg" is a placeholder for my actual package name here):
Warning messages:
1: replacing previous import 'heatmaply::normalize' by 'igraph::normalize' when loading 'myPkg'
2: replacing previous import 'igraph::decompose' by 'stats::decompose' when loading 'myPkg'
3: replacing previous import 'igraph::spectrum' by 'stats::spectrum' when loading 'myPkg'
Notably, I'm not actually using any of the conflicting functions. But because the entire package is listed as a dependency, the conflicts are an issue. Is there an elegant way to solve this? I know that I can use import::from() inline to import only the functions I need, but I prefer not to do that because inline imports are considered poor practice.
I have solved the problem. I was able to fix it by doing the following:
Removing all #import statements at the beginning of the functions I defined.
Including pkgName:: before each function call.
Related
Importing other packages' functions within one's own package is easy enough (example), but that requires having a hard dependency (Imports in the DESCRIPTION file) on that package. In my case, it is a suggested package, so I am getting a warning: '::' or ':::' import not declared on R CMD check.
So right now, I am recreating the functions locally, but this is problematic as when both packages are loaded, there is another warning The following objects are masked from [the other package]. They're the same, so it's not a big issue, but enough to be annoying. I can't imagine there's not a better practice than this?
In case we need actual code for demo, here is the problematic imports for my package: https://github.com/rempsyc/lavaanExtra/blob/main/R/save_as_x.R
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!
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.
My question is: How do I selectively import functions from two packages in the case where two of the packages have functions named DanielPlot (which is one of the functions I wish to import). I would like to import the DanielPlot function from the FrF2 package, but the BsMD package also has a function called DanielPlot. I tried selectively importing the functions I want from each package, but it does not work. A portion of my DESCRIPTION file is below:
Depends:
lattice
Imports: FrF2,
BsMD
and a portion of my NAMESPACE file is:
import(lattice)
importFrom(FrF2,DanielPlot)
importFrom(FrF2, IAPlot)
importFrom(FrF2, MEPlot)
importFrom(FrF2, pb)
importFrom(FrF2, FrF2)
importFrom(BsMD, BsProb)
importFrom(BsMD, LenthPlot)
importFrom(BsMD, BsMD)
When I try to check the package I get the message
Warning messages:
replacing previous import by ‘BsMD::DanielPlot’ when loading ‘daewr’
How can I avoid this warning?
When loading the RTextTools package from CRAN, I get the following warnings:
Warning messages:
1: replacing previous import ‘head’ when loading ‘utils’
2: replacing previous import ‘tail’ when loading ‘utils’
How do I get rid of these warnings? I'm the author of the package, so I can manipulate the source code; I'm looking for a solution that gets rid of the warnings rather than suppresses them. They seem to have appeared when I upgraded to R 2.14. Thank you in advance!
In general, this problem is often caused by having import(somepackage) in the namespace as well as importFrom(somepackage, somefunction).
Equivalently, using roxygen2, having both #' #import somepackage and #' #importFrom somepackage somefunction.
The best practice solution is to remove the import statement and keep only importFrom.
This is not your issue - it's an issue in the glmnet package that you depend on: it explicitly imports all functions from both Matrix and utils but in the wrong order which causes a conflict since they both define head and tail (Matrix depends on utils so utils must be first). It is easy to fix - the order of imports has to be reversed in the glmnet/NAMESPACE but only the maintainer of glmnet can do that.
PS: This would be better asked on R-devel
When loading the RTextTools package from CRAN, I get the following warnings:
Warning messages:
1: replacing previous import ‘head’ when loading ‘utils’
2: replacing previous import ‘tail’ when loading ‘utils’
How do I get rid of these warnings? I'm the author of the package, so I can manipulate the source code; I'm looking for a solution that gets rid of the warnings rather than suppresses them. They seem to have appeared when I upgraded to R 2.14. Thank you in advance!
In general, this problem is often caused by having import(somepackage) in the namespace as well as importFrom(somepackage, somefunction).
Equivalently, using roxygen2, having both #' #import somepackage and #' #importFrom somepackage somefunction.
The best practice solution is to remove the import statement and keep only importFrom.
This is not your issue - it's an issue in the glmnet package that you depend on: it explicitly imports all functions from both Matrix and utils but in the wrong order which causes a conflict since they both define head and tail (Matrix depends on utils so utils must be first). It is easy to fix - the order of imports has to be reversed in the glmnet/NAMESPACE but only the maintainer of glmnet can do that.
PS: This would be better asked on R-devel