loadedNamespaces
Loading and Unloading Namespaces

### Description

Provides for namespace management.

### Usage

    loadNamespace(package, lib.loc = NULL, keep.source =
        getOption("keep.source.pkgs"), partial = FALSE)
    requireNamespace(package, ..., quietly = FALSE)
    unloadNamespace(ns)
    attachNamespace(ns, pos = 2, depends = NULL, warn.conflicts = TRUE)
    loadedNamespaces()
    isNamespaceLoaded(name)

### Arguments

package | a character string or name. Specifies the name of the package to be loaded as a namespace.
lib.loc | a character vector. Specifies the names of library directories. If lib.loc is NULL, .libPaths is used.
keep.source | a logical value indicating whether the source code including comments are kept while loading a namespace. The default is the value of the option "keep.source.pkgs".
partial | a logical value. If TRUE, the function is returned after making and registering the namespace and (optionally) loading the source code. If FALSE (the default), more additional operations such as running hook functions, and so on are completed before the function is returned.
ns | a character string. Represents the namespace or the namespace object.
name | a character string. Represents the namespace or the namespace object.
pos | an integer value. Represents the position of the namespace to be attached in the search list.
depends | a character vector. Represents dependent namespaces to assign to the ".Depends" variable in the attached environment.
... | optional arguments to pass to loadNamespace. For example, lib.loc, keep.source, partial, and so on.
quietly | a logical value. If TRUE, the error message is not shown a namespace fails to load. The default is FALSE.
warn.conflicts | a logical value. If TRUE (the default), gives warning messages if the new objects conflict with the objects in the attached packages.

### Details

- Use loadNamespace to create a namespace for a package, and then load the package explicitly. loadNamespace creates and registers the namespace.
  - If the package has imported variables, classes, and methods, their namespaces are loaded implicitly.
  - The namespace information of a package exists in "<package_path>/ Meta/nsInfo.rds" and/or "<package_path>/NAMESPACE". The first file is used if both exist.
  - If the package has dependent methods listed in the "Depends" section of "<package_path>/ Meta/package.rds", the "methods" namespace is also loaded.
  - You can specify loading the source code by setting keep.source to TRUE.
  - If partial is FALSE, you can complete additional operations before the function is returned: for example, lazy-loading a database file, registering S3 methods listed in the namespace information file, loading dynamic libraries, assigning native routines, adding a namespace for dynamic libraries, running the hook function ".onLoad", updating the namespace exports, sealing the namespace, and so on.
  - loadNamespace does not attach the specified package to the search list after loading it. To attach the package, call attachNamespace. attachNamespace calls attach with "package:namespace" to create a new environment, and imports the "exports" fields of the specified namespace into the new environment.
  - After adding the "path" attribute (that is, the namespace path), lazy-loading the database, and assigning the ".Depends" variable to the environment, the environment is locked and invisibly returned. Also, the hook function ".onAttach" is run.
- Use requireNamespace in place of loadNamespace to specify that the namespace is required.
  - If the namespace for package is not already loaded, then requireNamespace calls loadNamespace and sets all its arguments except quietly.
  - requireNamespace catches any errors thrown by loadNamespace, prints the error messages (without aborting) if quietly is set to TRUE, and returns TRUE or FALSE indicating if the namespace is usable.
- Use unloadNamespace unload a namespace. If the namespace is attached in the search list, it is detached.
  - If the namespace is imported by other namespaces and is in use, the function stops with a message.
  - The hook function ".onUnload" is run, the specified namespace is unregistered, and the lazy-loaded database file is released.

Value

loadNamespace | returns the loaded name space object itself.
requireNamespace | returns TRUE invisibly if the namespace is loaded (that is, either if it was previously loaded or if it was loaded by this call). It returns FALSE if the name space cannot be loaded.
unloadNamespace | returns nothing invisibly.
attachNamespace | returns an invisible environment with "name" and "path" attributes (package name and path).
loadedNamespaces | returns a character vector with the names of the loaded name spaces.
isNamespaceLoaded | returns TRUE if name is the name of a currently-loaded namespace. Returns FALSE if name is not the name of a currently-loaded namespace.

See Also
isNamespace, attach, library.
Examples

    loadedNamespaces()
    ns_stats4 <- loadNamespace("stats4")
    env <- attachNamespace(ns_stats4, pos = 3,  depends = c("abc", "def"))
    env
    search()  # search list
    get(".Depends", env = env)
    loadedNamespaces()
    unloadNamespace(ns_stats4) # equivalent to: unloadNamespace("stats4")
    if (requireNamespace("Matrix")) {
        # use quality general version of expm if available
        Matrix::expm
    } else {
        # use homegrown version that works on nice symmetric matrices
        function(x) {
            eig <- eigen(x)
            eig$vectors %*% diag(exp(eig$values)) %*% t(Conj(eig$vectors))
        }
    }
    attachNamespace("stats4")
    # Error:  name space is already attached
    #attachNamespace("stats4", pos = 1)
    loadedNamespaces()
    unloadNamespace("stats4")
    # Error:  name space "graphics" is imported by "stats"
    # so it cannot be unloaded
    #unloadNamespace("graphics")

Package base version 6.1.5-18
Package Index