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.
attach(what, pos = 2L, name = deparse(substitute(what), backtick=FALSE),
warn.conflicts = TRUE)
| Parameter | Description |
|---|---|
what |
‘database’. This can be a data.frame or a list or a R data file created with save or NULL or an environment. See also ‘Details’. |
pos |
integer specifying position in search() where to attach. |
name |
name to use for the attached database. Names starting with package: are reserved for library. |
warn.conflicts |
logical. If TRUE, warnings are printed about conflicts 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. |
# NOT RUN {
require(utils)
summary(women$height) # refers to variable 'height' in the data frame
attach(women)
summary(height) # The same variable now available by name
height <- height*2.54 # Don't do this. It creates a new variable
# in the user's workspace
find("height")
summary(height) # The new variable in the workspace
rm(height)
summary(height) # The original variable.
height <<- height*25.4 # Change the copy in the attached environment
find("height")
summary(height) # The changed copy
detach("women")
summary(women$height) # unchanged
# }
# NOT RUN {
## create an environment on the search path and populate it
sys.source("myfuns.R", envir = attach(NULL, name = "myfuns"))
# }