Writing an R package: should `:: ` be used for every external function? – Package development

I don’t think this advice is correct in general. It may be correct for their particular context, I don’t know.

I thinks that in general this is somewhat subjective, and there are some tradeoffs to consider:

  • If you have name clashes (e.g. you use both pkg1::fun and pkg2::fun), then you need to use ::, you cannot import both functions.
  • If you use packages with function names that do not make it clear which packages they are in, then :: will make your code easier to read.
  • If you always use :: for a dependency that is not essential, then that dependency will not be loaded with your package (only when you first actually use it). This will make your package load faster. Sometimes much faster, because some popular packages load slowly, e.g. Matrix.
  • :: is slower than importing the function via NAMESPACE. (Haven’t checked this recently, but I am pretty sure that it still holds.)
  • Importing means shorter code, so it is quicker to type. Sounds banal, but if you call a package many times, it does matter.

You should never use library(foo) in a package, you use Imports in DESCRIPTION and the NAMESPACE file instead (or roxygen2 to generate the NAMESPACE file). Or Imports and the :: notation if you go that way.

Read more here: Source link