@@ -43,7 +43,9 @@ create_ns_env <- function(pkg = ".") {
43
43
env
44
44
}
45
45
46
- # This is taken directly from base::loadNamespace() in R 2.15.1
46
+ # This is taken directly from base::loadNamespace() in R 2.15.1.
47
+ # Except .Internal(registerNamespace(name, env)) is replaced by
48
+ # register_namespace(name, env)
47
49
makeNamespace <- function (name , version = NULL , lib = NULL ) {
48
50
impenv <- new.env(parent = .BaseNamespaceEnv , hash = TRUE )
49
51
attr(impenv , " name" ) <- paste(" imports" , name , sep = " :" )
@@ -62,7 +64,7 @@ makeNamespace <- function(name, version = NULL, lib = NULL) {
62
64
setNamespaceInfo(env , " dynlibs" , NULL )
63
65
setNamespaceInfo(env , " S3methods" , matrix (NA_character_ , 0L , 3L ))
64
66
assign(" .__S3MethodsTable__." , new.env(hash = TRUE , parent = baseenv()), envir = env )
65
- .Internal(registerNamespace( name , env ) )
67
+ register_namespace( name , env )
66
68
env
67
69
}
68
70
@@ -139,3 +141,60 @@ is_loaded <- function(pkg = ".") {
139
141
pkg <- as.package(pkg )
140
142
pkg $ package %in% loadedNamespaces()
141
143
}
144
+
145
+
146
+ # Returns the namespace registry
147
+ # ' @useDynLib devtools nsreg
148
+ ns_registry <- function () {
149
+ .Call(nsreg )
150
+ }
151
+
152
+
153
+ # Register a namespace
154
+ register_namespace <- function (name = NULL , env = NULL ) {
155
+ # Be careful about what we allow
156
+ if (! is.character(name ) || name == " " || length(name ) != 1 )
157
+ stop(" 'name' must be a non-empty character string." )
158
+
159
+ if (! is.environment(env ))
160
+ stop(" 'env' must be an environment." )
161
+
162
+ if (name %in% loadedNamespaces())
163
+ stop(" Namespace " , name , " is already registered." )
164
+
165
+ # Add the environment to the registry
166
+ nsr <- ns_registry()
167
+ nsr [[name ]] <- env
168
+
169
+ env
170
+ }
171
+
172
+
173
+ # unregister a namespace - should be used only if unloadNamespace()
174
+ # fails for some reason
175
+ unregister_namespace <- function (name = NULL ) {
176
+ # Be careful about what we allow
177
+ if (! is.character(name ) || name == " " || length(name ) != 1 )
178
+ stop(" 'name' must be a non-empty character string." )
179
+
180
+ if (! (name %in% loadedNamespaces()))
181
+ stop(name , " is not a registered namespace." )
182
+
183
+ # Remove the item from the registry
184
+ rm(name , ns_registry())
185
+ invisible ()
186
+ }
187
+
188
+ # This is similar to getNamespace(), except that getNamespace will load
189
+ # the namespace if it's not already loaded. This function will not.
190
+ # In R 2.16, a function called .getNamespace() will have the same effect
191
+ # and this will no longer be necessary.
192
+ get_namespace <- function (name ) {
193
+ # Sometimes we'll be passed something like as.name(name), so make sure
194
+ # it's a string
195
+ name <- as.character(name )
196
+ if (! (name %in% loadedNamespaces()))
197
+ return (NULL )
198
+ else
199
+ return (getNamespace(name ))
200
+ }
0 commit comments