2

Is it possible to manually create and load a namespace environment foo (without making an actual foo package), so that we can use the double colon operator foo::test to access objects in this environment?

The attach function attaches a list or environment to the search path:

foo <- list(test=123)
attach(foo)
get("test", "foo")

But this does give the user access to foo::test obviously.

Thomas
  • 43,637
  • 12
  • 109
  • 140
Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207

1 Answers1

1

It turns out that this is possible with the largely undocumented namespace package:

library(namespace)
ns <- makeNamespace("myNamespace")
assign("test", 7, env=ns)
setNamespaceInfo(ns, "exports", as.environment(list(test="test")))
print(myNamespace::test)

Not sure how safe this is though. For example sessionInfo doesn't work anymore:

> sessionInfo()
Error in if (pkgpath == "") { : argument is of length zero
Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207