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
DESCRIPTIONas well asNAMESPACEEach package listed in
DESCRIPTIONImports:must have a matching entryNAMESPACEimport(...). The entry inDESCRIPTIONmay contain version information, but inNAMESPACEyou only name the package.Note for the unwary: Spell
Importswith capitalIand trailingsin DESCRIPTIONFor example:
DESCRIPTION
NAMESPACE
2. Order matters
Packages that you
loadorimportlater 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
latticeandggplot2export alayerfunction. Thus if you first importlatticeand thenggplot2, it means thatggplot2::layerwill masklattice::layer.In other words, using
layerwill refer toggplot2::layer. If you want to refer to thelatticeversion you need to explicitly refer tolattice::layerin 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
reshapepackage, which depends onplyrbut also masks some functions inplyr: