\HeaderA{detach}{Detach Objects from the Search Path}{detach}
\keyword{data}{detach}
\begin{Description}\relax
Detach a database, i.e., remove it from the \code{\LinkA{search}{search}()} path
of available \R{} objects.  Usually, this is either a \code{\LinkA{data.frame}{data.frame}}
which has been \code{\LinkA{attach}{attach}}ed or a package which was required
previously.
\end{Description}
\begin{Usage}
\begin{verbatim}
detach(name, pos = 2, version, unload = FALSE)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{name}] The object to detach.  Defaults to \code{search()[pos]}.
This can be an unquoted name or a character string but \emph{not} a
character vector.  If a number is supplied this is taken as \code{pos}.

\item[\code{pos}] Index position in \code{\LinkA{search}{search}()} of database to
detach.  When \code{name} is a number, \code{pos = name}
is used.

\item[\code{version}] A character string denoting a version number of the
package to be removed.  This should be used only with a versioned
installation of the package: see \code{\LinkA{library}{library}}.

\item[\code{unload}] A logical value indicating whether or not to attempt to
unload the namespace and S4 methods when a package is being
detached.  If the package has a namespace and \code{unload} is
\code{TRUE}, then \code{detach} will attempt to unload the
namespace and remove any S4 methods defined by the package.  If
the namespace is in use or \code{unload} is \code{FALSE}, no
unloading will occur.

\end{ldescription}
\end{Arguments}
\begin{Details}\relax
This 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 \code{package:tools}.

When a package have been loaded with an explicit version number it can
be detached using the name shown by \code{\LinkA{search}{search}} or by
supplying \code{name} and \code{version}: see the examples.

If a package has a namespace, detaching it does not by default unload
the namespace (and may not even with \code{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 \code{\LinkA{library}{library}} on a package whose name
space is loaded, it attaches the exports of the loaded name space.
So detaching and re-attaching a package may not refresh some
or all components of the package, and is inadvisable.
\end{Details}
\begin{Value}
The attached database is returned invisibly, either as
\code{\LinkA{data.frame}{data.frame}} or as \code{\LinkA{list}{list}}.
\end{Value}
\begin{Note}\relax
You cannot detach either the workspace (position 1) or the \pkg{base}
package (the last item in the search list).
\end{Note}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{attach}{attach}}, \code{\LinkA{library}{library}}, \code{\LinkA{search}{search}},
\code{\LinkA{objects}{objects}}, \code{\LinkA{unloadNamespace}{unloadNamespace}},
\code{\LinkA{library.dynam.unload}{library.dynam.unload}} .
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
require(splines) # package
detach(package:splines)
## could equally well use detach("package:splines")
## but NOT  pkg <- "package:splines"; detach(pkg)
## Instead, use
library(splines)
pkg <- "package:splines"
detach(pos = match(pkg, search()))

## careful: do not do this unless 'splines' is not already loaded.
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])
   eval(substitute(detach(n), list(n=name)))
}
attach_and_detach(women, pos=3)

## Not run: 
## Using a versioned install
library(ash, version="1.0-9")  # or perhaps just library(ash)
# then one of
detach("package:ash", version="1.0-9")
# or
detach("package:ash_1.0-9")
## End(Not run)\end{ExampleCode}
\end{Examples}

