
search()
path of available R objects. Usually this is either a
data.frame
which has been attach
ed or a
package which was attached by library
.detach(name, pos = 2L, unload = FALSE, character.only = FALSE,
force = FALSE)
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
.
search()
of the database to
detach. When name
is a number, pos = name
is used.
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.
name
can be assumed to be a character string.NULL
when a
package is detached, otherwise the environment which was returned by
attach
when the object was attached (incorporating any
changes since it was attached).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
call. Use of attach
/detach
is best avoided in functions (see
the help for attach
) and in interactive use and scripts
it is prudent to detach by name.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). Further, registered S3 methods from the namespace will
not be removed. If you use library
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.attach
, library
, search
,
objects
, unloadNamespace
,
library.dynam.unload
.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 <- deparse(substitute(db))
attach(db, pos = pos, name = name)
print(search()[pos])
detach(name, character.only = TRUE)
}
attach_and_detach(women, pos = 3)
Run the code above in your browser using DataLab