loki loki - 1 year ago 86
R Question

Name space of base package needed?

Writing an R-package I use name spaces to use functions from existing packages, e.g.


However, I am wondering if functions from the
package have also be used like this, e.g.
. This might end up in very confusing code parts:

foo[base::which(base::sapply(bar, function())]

Answer Source

No you don't need to reference base packages like this. You only need to reference non-base packages to ensure they are loaded into the function environment when functions from your package are run, either by using :: or @import in the Roxegen notes at the top of your script. See why you don't need to reference base packages below:


"The distinction between the binding environment and the enclosing environment is important for package namespaces. Package namespaces keep packages independent. For example, if package A uses the base mean() function, what happens if package B creates its own mean() function? Namespaces ensure that package A continues to use the base mean() function, and that package A is not affected by package B (unless explicitly asked for)."(Hadley Wickham)

The only time you need to reference base:: is if the namespace for your package contains a package that has an alternative function of the same name.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download