(lns <- loadedNamespaces())
statL <- isNamespaceLoaded("stats")<!-- % checks work also when it is *not* loaded -->
stopifnot( identical(statL, "stats" %in% lns) )
## The string "foo" and the symbol 'foo' can be used interchangably here:
stopifnot( identical(isNamespaceLoaded( "foo" ), FALSE),
identical(isNamespaceLoaded(quote(foo)), FALSE),
identical(isNamespaceLoaded(quote(stats)), statL))
hasM <- isNamespaceLoaded("MASS") # (to restore if needed)
Mns <- asNamespace("MASS") # loads it if not already
stopifnot( isNamespaceLoaded("MASS"))
unloadNamespace(Mns) # unloading the NS 'object'
stopifnot( ! isNamespaceLoaded("MASS"))
if (hasM) loadNamespace("MASS") # (restoring previous state)
Run the code above in your browser using DataLab