[R-pkg-devel] Force namespace prefix for a loaded package function

Tim Keitt tkeitt at utexas.edu
Sun Jun 26 19:23:28 CEST 2016


It would be rather nice if we could define functions in our packages that
must be called with the namespace prefix.

I'd like to do

#' @protected (or some such)
f = function(...) list(...)

in package scope and then

library(x)
f(1)             # fails
x::f(1)         # succeeds

Currently unless I am missing something, a function is either exported to
global scope or not available. This could be done if package loading made
two environments, one in the path and another not in the path, and then
have the namespace prefix search both in succession.

Yes, you could do

#' @export
x_f = function(...) list(...)

library(x)
x_f(1)

but I would prefer reusing the namespace prefix syntax.

This would also avoid name collisions between package, which ideally is the
purpose of a namespace.

I suppose also you could make two packages and list one in Imports: but I
find that less satisfying because it requires a different namespace prefix.

Or am I missing something obvious here.

THK

http://www.keittlab.org/

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list