Warning: Replacing Previous Import 'Head' When Loading 'Utils' in R

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

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

r- install package returned non-zero exit status warning

I managed to resolve it by installing readxl. One of the warnings states that there is no package called 'readxl'

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

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.

Importing two functions with same name using roxygen2

The thing to keep in mind is that you cannot have more than one function with
the same name in your package's namespace.

Suppose there are two packages, pkgA and pkgB, that both export a function
called foo. If you create a package, pkgC, that has import(pkgA) and
import(pkgB) in the NAMESPACE. Now, when you call library(pkgC) you'll get
a warning:

replacing previous import 'foo' when loading 'pkgB'. 

Now, suppose someone creates another package, pkgD, that has this in the NAMESPACE file:

import(pkgA)
import(pkgB)
import(pkgC)

Then, library(pkgD) will give 2 warnings:

1: replacing previous import ‘foo’ when loading ‘pkgB’ 
2: replacing previous import ‘foo’ when loading ‘pkgB’

If everyone adopts the practice of importing entire namespaces, then 30 years
from now, there will be a lot of these warnings.

Instead, since you can only have a single "foo" in your package, you should
explicitly import the "foo" (and other functions) that you want your package
to use. In the above example, the NAMESPACE for pkgD should be

importFrom(pkgB,foo)

If you actually need to use the two functions with the same name from two different packages, one hack you can perform is to import other functions from each package to ensure the packages are installed and their namespaces are loaded, but then refer to the functions you need using :: notation by placing this in your NAMESPACE:

importFrom(pkgA,foo)
importFrom(pkgB,bar)

and then calling functions pkgA::abc() and pkgB::abc() in your code.

R import all but a couple of functions

Currently my best idea is

all <- getNamespaceExports("grid")
paste("@importFrom grid", paste(all[!(all %in% c("arrow", "unit"))], collapse = " "))
#[1] "@importFrom grid grid.edit pop.viewport ...

That's obviously not a good solution, but unlike for exports you can't use a regex for imports, i.e., there is no importPatternFrom.

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

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.



Related Topics



Leave a reply



Submit