R: Loading and Unloading Namespaces

ns-load {base} | R Documentation

## Loading and Unloading Namespaces

### Description

Functions to load and unload namespaces.

### Usage

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

### Arguments

`ns` | string or namespace object.
`pos` | integer specifying position to attach.
`dataPath` | path to directory containing a database of datasets to be lazy-loaded into the attached environment.
`depends` | `NULL` or a character vector of dependencies to be recorded in object `.Depends` in the package.
`package` | string naming the package/namespace to load.
`lib.loc` | character vector specifying library search path.
`keep.source` | Now ignored except during package installation. For more details see this argument to `library`.
`partial` | logical; if true, stop just after loading code.
`quietly` | logical: should progress and error messages be suppressed?
`...` | further arguments to be passed to `loadNamespace`.

### Details

The functions `loadNamespace` and `attachNamespace` are usually called implicitly when `library` is used to load a name space and any imports needed. However it may be useful to call these functions directly at times.

`loadNamespace` loads the specified namespace and registers it in an internal data base. A request to load a namespace when one of that name is already loaded has no effect. The arguments have the same meaning as the corresponding arguments to `library`, whose help page explains the details of how a particular installed package comes to be chosen. After loading, `loadNamespace` looks for a hook function named `.onLoad` as an internal variable in the namespace (it should not be exported). This function is called with the same arguments as `.First.lib`. Partial loading is used to support installation with the --lazy option.

`loadNamespace` does not attach the namespace it loads to the search path. `attachNamespace` can be used to attach a frame containing the exported values of a namespace to the search path (but this is almost always done via `library`). The hook function `.onAttach` is run after the namespace exports are attached.

`requireNamespace` is a wrapper for `loadNamespace` analogous to `require` that returns a logical value.

`loadedNamespaces` returns a character vector of the names of the loaded namespaces.

`unloadNamespace` can be used to attempt to force a namespace to be unloaded. If the namespace is attached, it is first `detach`ed, thereby running a `.Last.lib` function in the namespace if one is exported. Then an error is signaled if the namespace is imported by other loaded namespaces, and the namespace is not unloaded. If defined, a hook function `.onUnload` is run before removing the namespace from the internal registry.

See the comments in the help for `detach` about some issues with unloading and reloading namespaces.

### Value

`attachNamespace` returns invisibly the package environment it adds to the search path.

`loadNamespace` returns the namespace environment, either one already loaded or the one the function causes to be loaded.

`requireNamespace` returns `TRUE` if it succeeds or `FALSE`.

`loadedNamespaces` returns a character vector.

`unloadNamespace` returns `NULL`, invisibly.

### Author(s)

Luke Tierney and R-core

[Package base version 2.15.1 Index]