Rev 85065 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/base/man/detach.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2014 R Core Team% Distributed under GPL 2 or later\name{detach}\alias{detach}\title{Detach Objects from the Search Path}\usage{detach(name, pos = 2L, unload = FALSE, character.only = FALSE,force = FALSE)}\arguments{\item{name}{the object to detach. Defaults to \code{search()[pos]}.This can be an unquoted name or a character string but \emph{not} acharacter vector. If a number is supplied this is taken as \code{pos}.}\item{pos}{index position in \code{\link{search}()} of the database todetach. When \code{name} is a number, \code{pos = name}is used.}\item{unload}{a logical value indicating whether or not to attempt tounload the namespace when a package is being detached. If thepackage has a namespace and \code{unload} is \code{TRUE}, then\code{detach} will attempt to unload the namespace \emph{via}\code{\link{unloadNamespace}}: if the namespace is imported byanother namespace or \code{unload} is \code{FALSE}, no unloadingwill occur.}\item{character.only}{a logical indicating whether \code{name}can be assumed to be a character string.}\item{force}{logical: should a package be detached even though otherattached packages depend on it?}}\description{Detach a database, i.e., remove it from the \code{\link{search}()}path of available \R objects. Usually this is either a\code{\link{data.frame}} which has been \code{\link{attach}}ed or apackage which was attached by \code{\link{library}}.}\details{This is most commonly used with a single number argument referring to aposition on the search list, and can also be used with a unquoted orquoted name of an item on the search list such as \code{package:tools}.If a package has a namespace, detaching it does not by default unloadthe namespace (and may not even with \code{unload = TRUE}), anddetaching will not in general unload any dynamically loaded compiledcode (DLLs); see \code{\link{getLoadedDLLs}} and\code{\link{library.dynam.unload}}. Further, registered S3 methodsfrom the namespace will not be removed, and because S3 methods arenot tagged to their source on registration, it is in general notpossible to safely un-register the methods associated with a givenpackage. If you use \code{\link{library}} on a package whosenamespace is loaded, it attaches the exports of the already loadednamespace. So detaching and re-attaching a package may not refreshsome or all components of the package, and is inadvisable. The mostreliable way to completely detach a package is to restart \R.}\section{Good practice}{\code{detach()} without an argument removes the first item on thesearch path after the workspace. It is all too easy to call it toomany or too few times, or to not notice that the search path haschanged since an \code{\link{attach}} call.Use of \code{attach}/\code{detach} is best avoided in functions (seethe help for \code{\link{attach}}) and in interactive use and scriptsit is prudent to detach by name.}\note{You cannot detach either the workspace (position 1) nor the \pkg{base}package (the last item in the search list), and attempting to do sowill throw an error.Unloading some namespaces has undesirable side effects:e.g.\sspace{}unloading \pkg{grid} closes all graphics devices, and on somesystems \pkg{tcltk} cannot be reloaded once it has been unloaded andmay crash \R if this is attempted.}\value{The return value is \link{invisible}. It is \code{NULL} when apackage is detached, otherwise the environment which was returned by\code{\link{attach}} when the object was attached (incorporating anychanges since it was attached).}\references{\bibshow{R:Becker+Chambers+Wilks:1988}}\seealso{\code{\link{attach}}, \code{\link{library}}, \code{\link{search}},\code{\link{objects}}, \code{\link{unloadNamespace}},\code{\link{library.dynam.unload}} .}\examples{require(splines) # packagedetach(package:splines)## or alsolibrary(splines)pkg <- "package:splines"\dontshow{stopifnot(inherits(tryCatch(detach(pkg), error = function(.).), "error"))}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 vectorattach_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)}\keyword{data}