NAMESPACE issue: multiple packages import and export an identical function - r

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?

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.

In an R Package, How do You Force Load Packages That Are Imports

My DESCRIPTION file requires a handful of packages (I've shown two below for simplicity):
Imports:
tidyverse,
magrittr
In my NAMESPACE file, I then use export(functions) for functions I WRITE, and I used import(tidyverse), import(magrittr) so ALL functions in these packages get loaded automatically. However, this isn't working and I still have to call library(tidyverse) after I load my package.
Any ideas?

Imports - functions with the same name but from different package

I'm working on update of my package. However I'm struggling with dependencies/imports. I use two conflicted packages - ggplot2 and psych and their functions alpha and of course alpha object of ggplot2 differs from alpha function of psych package.
When building package I get an warning:
Warning: replacing previous import 'ggplot2::alpha' by 'psych::alpha' when loading 'ShinyItemAnalysis'
Hence I wont be able to publish my package on CRAN (untill I solved this warning). Is there any easy way how to avoid this import conflict?
Note that when using roxygen2 package to build NAMESPACE from fellow .R scripts, it is very helpful to delete the NAMESPACE file and run roxygen2 again. Solves many problems.

R package selective import and namespace

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?

Order of DESCRIPTION Imports: and NAMESPACE import() in R 2.14.0 package checking

I'm trying to chase down what seems to be a conflict between function names as I check a package. I may eventually ask directly about the problem, but first, I am wondering about three things, none of which seem to be mentioned in R-exts:
The packages listed in DESCRIPTION: Imports and NAMESPACE imports() should be the same, right?
Within either list, does the order of importing matter? If so, is there any general advice about how to order them?
I use R --vanilla CMD check pkg_name to avoid having my .Rprofile interfere. When my .Rprofile is active and contains library(pkg_name) statements, does the order of those matter?
You asked three questions.
1. List packages in DESCRIPTION as well as NAMESPACE
Each package listed in DESCRIPTION Imports: must have a matching entry NAMESPACE import(...). The entry in DESCRIPTION may contain version information, but in NAMESPACE you only name the package.
Note for the unwary: Spell Imports with capital I and trailing s in DESCRIPTION
For example:
DESCRIPTION
Imports:
stringr (>= 0.5)
NAMESPACE
import(stringr)
2. Order matters
Packages that you load or import later masks packages that were loaded or imported earlier. This only matters if you import packages that have export a function with identical name.
For example, both lattice and ggplot2 export a layer function. Thus if you first import lattice and then ggplot2, it means that ggplot2::layer will mask lattice::layer.
In other words, using layer will refer to ggplot2::layer. If you want to refer to the lattice version you need to explicitly refer to lattice::layer in your function.
3. The order of loading packages also matter
For the same reason, the order of loading packages (either in a script or in .Rprofile) matters. Any new package that you load will mask functions with the same name in previously loaded packages.
When this happens, R does the sensible thing and tells you about it in a console message.
Here is an example of masking that occurs when loading the reshape package, which depends on plyr but also masks some functions in plyr:
library(reshape)
Loading required package: plyr
Attaching package: 'plyr'
The following object(s) are masked from 'package:braidppt':
.
Attaching package: 'reshape'
The following object(s) are masked from 'package:plyr':
rename, round_any

Resources