Detach a database, i.e., remove it from the search() path of available R objects. Usually this is either a data.frame which has been attached or a package which was attached by library.
detach(name, pos = 2L, unload = FALSE, character.only = FALSE,
force = FALSE)
| Parameter | Description |
|---|---|
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 = name is 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, then detach 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 name can be assumed to be a character string. |
force |
logical: should a package be detached even though other attached packages depend on it? |
# NOT RUN {
require(splines) # package
detach(package:splines)
## or also
library(splines)
pkg <- "package:splines"
# }
# NOT RUN {
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 <- deparse(substitute(db))
attach(db, pos = pos, name = name)
print(search()[pos])
detach(name, character.only = TRUE)
}
attach_and_detach(women, pos = 3)
# }