Namespaces are environments, so you can use exactly the same mechanism. Since R uses a lexical definition, the parent element of the environment determines what the function sees (for example, how free variables are bound). And just like a namespace, you can attach environments and search for them.
So, to create a new βmanual namespaceβ, you can use something like
e <- new.env(parent=baseenv()) # use local(), sys.source(), source() or e$foo <- assignment to populate it, eg local({ f <- function() { ... } #... }, e) attach(e, name = "mySuperNamespace")
Now it loads and attaches in the same way as a namespace, so you can use f in the same way as in a namespace. Namespaces use another parent environment to allow imports β you can do this too if you want. If you need to check your cool environment, just check the search path, for example "mySuperNamespace" %in% search() . If you need a real environment, use as.environment("mySuperNamespace")
source share