Skip to content

Instantly share code, notes, and snippets.

@maurolepore
Created November 10, 2017 15:59
Show Gist options
  • Save maurolepore/a3ae5ed256af7556bbb19e46de124713 to your computer and use it in GitHub Desktop.
Save maurolepore/a3ae5ed256af7556bbb19e46de124713 to your computer and use it in GitHub Desktop.
Minimum information to deal with NAMESPACE issues.

Read this if one of the following applies to you:

  • You are using library(package) or require(package) inside your functions
  • You don't know exactly when you need the syntax package::function() or package:::function() as opposed to function().

If you are using functions from other packages inside functions of your own package, then you are dealing with NAMESPACE issues. NAMESPACE is one of the most confusing parts of an R package. Prepare for your brain to hurt a bit. This information will not answer all your questions, but it will rise an alarm that may save you time and trouble.

From http://r-pkgs.had.co.nz/namespace.html, find section "Imports" and read its introduction and also the subheading "R functions".

R functions

If you are using just a few functions from another package, my recommendation is to note the package name in the Imports: field of the DESCRIPTION file and call the function(s) explicitly using ::, e.g., pkg::fun().

If you are using functions repeatedly, you can avoid :: by importing the function with @importFrom pgk fun. This also has a small performance benefit, because :: adds approximately 5 µs to function evaluation time.

Alternatively, if you are repeatedly using many functions from another package, you can import all of them using @import package. This is the least recommended solution because it makes your code harder to read (you can’t tell where a function is coming from), and if you @import many packages, it increases the chance of conflicting function names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment