Detach Objects from the Search Path (original) (raw)
detach {base} | R Documentation |
---|
Description
Detach a database, i.e., remove it from the [search](../../base/help/search.html)()
path of available R objects. Usually this is either a[data.frame](../../base/help/data.frame.html)
which has been [attach](../../base/help/attach.html)
ed or a package which was attached by [library](../../base/help/library.html)
.
Usage
detach(name, pos = 2L, unload = FALSE, character.only = FALSE,
force = FALSE)
Arguments
name | the object to detach. Defaults to search()[pos]. This can be an unquoted name or a character string but not a character vector. If a number is supplied this is taken as pos. |
---|---|
pos | index position in search() of the database to detach. When name is a number, pos = nameis used. |
unload | a logical value indicating whether or not to attempt to unload the namespace when a package is being detached. If the package has a namespace and unload is TRUE, thendetach will attempt to unload the namespace via unloadNamespace: if the namespace is imported by another namespace or unload is FALSE, no unloading will occur. |
character.only | a logical indicating whether namecan be assumed to be a character string. |
force | logical: should a package be detached even though other attached packages depend on it? |
Details
This is most commonly used with a single number argument referring to a position on the search list, and can also be used with a unquoted or quoted name of an item on the search list such as package:tools
.
If a package has a namespace, detaching it does not by default unload the namespace (and may not even with unload = TRUE
), and detaching will not in general unload any dynamically loaded compiled code (DLLs); see [getLoadedDLLs](../../base/help/getLoadedDLLs.html)
and[library.dynam.unload](../../base/help/library.dynam.unload.html)
. Further, registered S3 methods from the namespace will not be removed, and because S3 methods are not tagged to their source on registration, it is in general not possible to safely un-register the methods associated with a given package. If you use [library](../../base/help/library.html)
on a package whose namespace is loaded, it attaches the exports of the already loaded namespace. So detaching and re-attaching a package may not refresh some or all components of the package, and is inadvisable. The most reliable way to completely detach a package is to restart R.
Value
The return value is invisible. It is NULL
when a package is detached, otherwise the environment which was returned by[attach](../../base/help/attach.html)
when the object was attached (incorporating any changes since it was attached).
Good practice
detach()
without an argument removes the first item on the search path after the workspace. It is all too easy to call it too many or too few times, or to not notice that the search path has changed since an [attach](../../base/help/attach.html)
call.
Use of attach
/detach
is best avoided in functions (see the help for [attach](../../base/help/attach.html)
) and in interactive use and scripts it is prudent to detach by name.
Note
You cannot detach either the workspace (position 1) nor the basepackage (the last item in the search list), and attempting to do so will throw an error.
Unloading some namespaces has undesirable side effects: e.g. unloading grid closes all graphics devices, and on some systems tcltk cannot be reloaded once it has been unloaded and may crash R if this is attempted.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language. Wadsworth & Brooks/Cole.
See Also
[attach](../../base/help/attach.html)
, [library](../../base/help/library.html)
, [search](../../base/help/search.html)
,[objects](../../base/help/objects.html)
, [unloadNamespace](../../base/help/unloadNamespace.html)
,[library.dynam.unload](../../base/help/library.dynam.unload.html)
.
Examples
require(splines) # package
detach(package:splines)
## or also
library(splines)
pkg <- "package:splines"
detach(pkg, character.only = TRUE)
## careful: do not do this unless 'splines' is not already attached.
library(splines)
detach(2) # 'pos' used for 'name'
## an example of the name argument to attach
## and of detaching a database named by a character vector
attach_and_detach <- function(db, pos = 2)
{
name <- deparse1(substitute(db))
attach(db, pos = pos, name = name)
print(search()[pos])
detach(name, character.only = TRUE)
}
attach_and_detach(women, pos = 3)
[Package _base_ version 4.6.0 Index]