Warning: replacing previous import ‘head’ when loading ‘utils’ in R - r

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

Related

What's the right way to import functions for suggested packages?

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

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.

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.

character(0) warnings when running devtools::load_all(".") in RStudio

I have an R package that I've been building in RStudio, let's called it my_pkg. When I run devtools::load_all(".") within RStudio (specifically using the Ctrl + Shift + L shortcut), I get the following message:
Loading my_pkg
Warning messages:
1: character(0)
2: character(0)
3: character(0)
4: character(0)
5: character(0)
All of the functions in the package work fine. My NAMESPACE and DESCRIPTION files are complete with no syntax errors. When I run ?my_pkg, however, the help file does not match the specifications provided in the DESCRIPTION file. When I remove the Imports from DESCRIPTION, there is no more character(0) warning message. Of course, I need those imports. When I change Imports to Suggests, there is character(0) warning message.
Here is the description file content, with some stuff changed to protect IP.
Package: scoutdroid
Title: This is where the title is.
Version: 0.1
Authors#R: "Ben Hanowell <benjamin.hanowell#redfin.com> [aut, cre]"
Description: This is where the description is.
Depends:
R (>= 3.1.0)
Imports:
dplyr,
lubridate,
mboost,
randomForestSRC,
RODBC,
stringr
License: file LICENSE
LazyData: true
And here is NAMESPACE.
# Generated by roxygen2 (4.0.1): do not edit by hand
import(RODBC)
import(dplyr)
import(lubridate)
import(mboost)
import(parallel)
import(randomForestSRC)
import(stringr)
When I use the RStudio Build & Reload button in the Build tab, I get the following warnings:
** preparing package for lazy loading
Warning: replacing previous import by 'lubridate::intersect' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::setdiff' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::union' when loading 'scoutdroid'
edit Added some more details to help folks understand what might be going on.
edit 2 I also added the DESCRIPTION file, although I don't provide the full package, which is proprietary.
edit 3 Added NAMESPACE.
edit 4 Added warnings that occur when using RStudio Build & Reload button in the Build tab.
After some dialoge in the comments, we figured out that the empty warnings that load_all is giving you are actually initiated when loading the package because of function name conflicts.
The issue is that you are importing a function from a package, then overwriting that function. When that happens R throws warnings as you saw when you clicked "Build & Reload" in RStudio:
Warning: replacing previous import by 'lubridate::intersect' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::setdiff' when loading 'scoutdroid'
Warning: replacing previous import by 'lubridate::union' when loading 'scoutdroid'
It looks like load_all may be attempting to muffle those warnings (just a guess) which is why you see character(0) instead of the actual warnings. (These particular warnings are difficult to silence.)
It is generally not a good idea to import an entire package's namespace. You should instead import only the symbols you need. See this post of mine for more.
The solution is to use importFrom instead of import in your NAMESPACE file.
It can also be due to a broken link in the roxygen2 documentation. For example when you link to a function external to your package with the wrong name, say \link[stringi]{STRI_C} instead of \link[stringi]{stri_c}

warning messages: replacing previous import by plyr:: when loading my package [duplicate]

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

Resources