[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  attach: Attach Set of R Objects to Search Path

##  Description

The database is attached to the  R  search path. This means that the database
is searched by  R  when evaluating a variable, so objects in the database can
be accessed by simply giving their names.

##  Usage

    
    
    attach(what, pos = 2L, name = deparse(substitute(what), backtick=FALSE),
           warn.conflicts = TRUE)

##  Arguments

what

‘database’. This can be a ` data.frame ` or a ` list ` or a  R  data file
created with ` [ save ](/link/save?package=base&version=3.6.2) ` or ` NULL `
or an environment. See also ‘Details’.

pos

integer specifying position in ` [ search
](/link/search?package=base&version=3.6.2) () ` where to attach.

name

name to use for the attached database. Names starting with ` package: ` are
reserved for ` [ library ](/link/library?package=base&version=3.6.2) ` .

warn.conflicts

logical. If ` TRUE ` , warnings are printed about ` [ conflicts
](/link/conflicts?package=base&version=3.6.2) ` from attaching the database,
unless that database contains an object ` .conflicts.OK ` . A conflict is a
function masking a function, or a non-function masking a non-function.

##  Value

The ` [ environment ](/link/environment?package=base&version=3.6.2) ` is
returned invisibly with a ` "name" ` attribute.

##  Good practice

` attach ` has the side effect of altering the search path and this can easily
lead to the wrong object of a particular name being found. People do often
forget to ` [ detach ](/link/detach?package=base&version=3.6.2) ` databases.

In interactive use, ` [ with ](/link/with?package=base&version=3.6.2) ` is
usually preferable to the use of ` attach ` / ` detach ` , unless ` what ` is
a ` [ save ](/link/save?package=base&version=3.6.2) () ` -produced file in
which case ` attach() ` is a (safety) wrapper for ` [ load
](/link/load?package=base&version=3.6.2) () ` .

In programming, functions should not change the search path unless that is
their purpose. Often ` [ with ](/link/with?package=base&version=3.6.2) ` can
be used within a function. If not, good practice is to

  * Always use a distinctive ` name ` argument, and 

  * To immediately follow the ` attach ` call by an ` [ on.exit ](/link/on.exit?package=base&version=3.6.2) ` call to ` detach ` using the distinctive name. 

This ensures that the search path is left unchanged even if the function is
interrupted or if code after the ` attach ` call changes the search path.

##  Details

When evaluating a variable or function name  R  searches for that name in the
databases listed by ` [ search ](/link/search?package=base&version=3.6.2) ` .
The first name of the appropriate type is used.

By attaching a data frame (or list) to the search path it is possible to refer
to the variables in the data frame by their names alone, rather than as
components of the data frame (e.g., in the example below, ` height ` rather
than ` women$height ` ).

By default the database is attached in position 2 in the search path,
immediately after the user's workspace and before all previously attached
packages and previously attached databases. This can be altered to attach
later in the search path with the ` pos ` option, but you cannot attach at `
pos = 1 ` .

The database is not actually attached. Rather, a new environment is created on
the search path and the elements of a list (including columns of a data frame)
or objects in a save file or an environment are _copied_ into the new
environment. If you use ` [ <<-
](/link/%26lt%3B%26lt%3B-?package=base&version=3.6.2) ` or ` [ assign
](/link/assign?package=base&version=3.6.2) ` to assign to an attached
database, you only alter the attached copy, not the original object. (Normal
assignment will place a modified version i