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.

Leave a Comment