When evaluating a variable or function name R searches for
that name in the databases listed by search
. 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
<<-< a=""><-<>
or assign
to assign to an attached
database, you only alter the attached copy, not the original object.
(Normal assignment will place a modified version in the user's
workspace: see the examples.) For this reason attach
can lead
to confusion.
One useful trick is to use what = NULL
(or equivalently a
length-zero list) to create a new environment on the search path into
which objects can be assigned by assign
or
load
or sys.source
.
Names starting "package:"
are reserved for
library
and should not be used by end users. Attached
files are by default given the name file:what
. The
name
argument given for the attached environment will be used
by search
and can be used as the argument to
as.environment
.
There are hooks to attach user-defined table objects of class
"UserDefinedDatabase"
, supported by the Omegahat package
RObjectTables. See http://www.omegahat.org/RObjectTables/.