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 asNAMESPACE
Each package listed in
DESCRIPTION
Imports:
must have a matching entryNAMESPACE
import(...)
. The entry inDESCRIPTION
may contain version information, but inNAMESPACE
you only name the package.Note for the unwary: Spell
Imports
with capitalI
and trailings
in DESCRIPTIONFor example:
DESCRIPTION
NAMESPACE
2. Order matters
Packages that you
load
orimport
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
andggplot2
export alayer
function. Thus if you first importlattice
and thenggplot2
, it means thatggplot2::layer
will masklattice::layer
.In other words, using
layer
will refer toggplot2::layer
. If you want to refer to thelattice
version you need to explicitly refer tolattice::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 onplyr
but also masks some functions inplyr
: