R package selective import and namespace - r

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?

Related

Conflicted package in R package building

I am creating my first package, here I am making some var estimations, the functions are running, however, I use packages that have the same function names.
Before writing the package I made an R script with the function and tests if it works, but at the top of my script I used the following code:
invisible(lapply(c("tibble","readxl","dplyr","stringr", "tidyr", "vars", "conflicted","forecast", "lubridate"), library, character.only = T))
conflict_prefer("select","dplyr")
conflict_prefer("lag", "dplyr")
conflict_prefer("filter", "dplyr")
The conflicted package chose the functions select, lag, and filter comes from the dplyr package rather from the stats package.
So I have not figured out how to use the conflict_prefer function inside the package.
Should they be the first lines of my function?
There is a roxygen way to prefer same-name functions?
I ask this because I get this warning:
> devtools::load_all()
i Loading FAVAR.MEF
Warning messages:
1: replacing previous import ‘dplyr::filter’ by ‘stats::filter’ when loading ‘FAVAR.MEF’
2: replacing previous import ‘dplyr::lag’ by ‘stats::lag’ when loading ‘FAVAR.MEF’
3: replacing previous import ‘stats::filter’ by ‘dplyr::filter’ when loading ‘FAVAR.MEF’
4: In setup_ns_exports(path, export_all, export_imports) :
Objects listed as exports, but not present in namespace: favar_est
Thanks in advance!!
If you are writing your own package and using external dependencies, you should not load them through repeated calls to library.
The proper way to do it is to state your dependencies in the DECRIPTION file of your package, which will mean that your dependencies are put on the search path in the correct order when your package is loaded. In your case, this removes the need for conflict_prefer, as dplyr will be higher up on the search path than stats. It also makes your package portable, because anyone who installs your package will have any missing dependencies installed automatically according to the packages listed in your DESCRIPTION file. Furthermore, doing it this way allows you to specify a minimum version of the dependency, so that anyone who already has an older version of the dependency installed will not come up against an obscure error when they try to use your package.
The DESCRIPTION file resides in the root directory of your package. It is a simple text file.
You need only add:
Depends:
tibble,
readxl,
dplyr,
stringr,
tidyr,
vars,
conflicted,
forecast,
lubridate
within this file, and your dependencies will be loaded with your package.

Resolve conflicting functions in R package DESCRIPTION file imports

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.

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

NAMESPACE issue: multiple packages import and export an identical function

I am writing a small package to do some routine data analysis and I wish to include some easy mapping functions. My package imports tidyr, dplyr, and leaflet (from RStudio on Github). When I build or load my package I get the warning:
replacing previous import by ‘leaflet::%>%’ when loading ‘MyPackage’
Looking at the NAMESPACE files of leaflet, dplyr and tidyr, I find that all three have
importFrom(magrittr,"%>%")
and
export("%>%")
So basically all 3 are importing and exporting the same function from the magrittr package. Is there some way I can resolve this within may package so that the warning is no longer generated (not just suppressed)? Is it something I should just ignore?

R: cannot find exported function even after importing entire namespace

I'm building a package, myPackage that imports from several other packages. The problem is that a function fun (it's actually a method) that is exported from another package otherPackage cannot be found when run. I did all of the following:
Imports: otherPackage and Depends: methods in myPackage's DESCRIPTION file
import(otherPackage) in myPackage's NAMESPACE file
Also tried adding importMethodsFrom(otherPackage,"fun")
The only way I found it to work is if I replace Imports: otherPackage with Depends: otherPackage, but we all know that it is recommended to import another package's namespace instead of attaching it to the search path. Can someone please explain why Importing it doesn't work?
Specifics: the function in question is keys from package AnnotationDbi in Bioconductor. The specific error is Error in .select(x, keys, columns, keytype) :
could not find function "keys"
Thank you.

Resources