loki loki - 1 month ago 14
R Question

Name space of base package needed?

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

raster::writeRaster(...)
.

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

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

Answer

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:

http://adv-r.had.co.nz/Environments.html

"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.