Return the Value of a Named Object (original) (raw)
get {base} | R Documentation |
---|
Description
Search by name for an object (get
) or zero or more objects (mget
).
Usage
get(x, pos = -1, envir = as.environment(pos), mode = "any",
inherits = TRUE)
mget(x, envir = as.environment(-1), mode = "any", ifnotfound,
inherits = FALSE)
dynGet(x, ifnotfound = , minframe = 1L, inherits = FALSE)
Arguments
x | For get, an object name (given as a character string or a symbol).For mget, a character vector of object names. |
---|---|
pos, envir | where to look for the object (see ‘Details’); if omitted search as if the name of the object appeared unquoted in an expression. |
mode | the mode or type of object sought: see the ‘Details’ section. |
inherits | should the enclosing frames of the environment be searched? |
ifnotfound | For mget, a list of values to be used if the item is not found: it will be coerced to a list if necessary.For dynGet any R object, e.g., a call tostop(). |
minframe | integer specifying the minimal frame number to look into. |
Details
The pos
argument can specify the environment in which to look for the object in any of several ways: as a positive integer (the position in the [search](../../base/help/search.html)
list); as the character string name of an element in the search list; or as an[environment](../../base/help/environment.html)
(including using [sys.frame](../../base/help/sys.frame.html)
to access the currently active function calls). The default of-1
indicates the current environment of the call toget
. The envir
argument is an alternative way to specify an environment.
These functions look to see if each of the name(s) x
have a value bound to it in the specified environment. If inherits
isTRUE
and a value is not found for x
in the specified environment, the enclosing frames of the environment are searched until the name x
is encountered. See [environment](../../base/help/environment.html)
and the ‘R Language Definition’ manual for details about the structure of environments and their enclosures.
If mode
is specified then only objects of that type are sought.mode
here is a mixture of the meanings of [typeof](../../base/help/typeof.html)
and [mode](../../base/help/mode.html)
: "function"
covers primitive functions and operators, "numeric"
, "integer"
and "double"
all refer to any numeric type, "symbol"
and "name"
are equivalent but "language"
must be used (and not"call"
or "("
). Currently, mode = "S4"
and mode = "object"
are equivalent.
For mget
, the values of mode
and ifnotfound
can be either the same length as x
or of length 1. The argumentifnotfound
must be a list containing either the value to use if the requested item is not found or a function of one argument which will be called if the item is not found, with argument the name of the item being requested.
dynGet()
is somewhat experimental and to be used _inside_another function. It looks for an object in the callers, i.e., the [sys.frame](../../base/help/sys.frame.html)()
s of the function. Use with caution.
Value
For get
, the object found. If no object is found an error results. If the object is the internal missing argument (aka R_MissingArg
in C), a classed error, class "getMissingError"
is signalled.
For mget
, a named list of objects (found or specified via ifnotfound
).
Note
The reverse (or “inverse”) of a <- get(nam)
is[assign](../../base/help/assign.html)(nam, a)
, assigning a
to name nam
.
inherits = TRUE
is the default for get
in Rbut not for S where it had a different meaning.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language. Wadsworth & Brooks/Cole.
See Also
[exists](../../base/help/exists.html)
for checking whether an object exists;[get0](../../base/help/get0.html)
for an efficient way of both checking existence and getting an object.
[assign](../../base/help/assign.html)
, the inverse of get()
, see above.
Use [getAnywhere](../../utils/help/getAnywhere.html)
for searching for an object anywhere, including in other namespaces, and[getFromNamespace](../../utils/help/getFromNamespace.html)
to find an object in a specific namespace.
Examples
get("%o%")
## test mget
e1 <- new.env()
mget(letters, e1, ifnotfound = as.list(LETTERS))
## very low-level: get()ing the "missing argument", e.g., inside browser()
ls.str(E <- environment((\(m) \(){})())) # m : <missing>
str(E$m) # (empty) symbol
ee <- tryCatch(get("m",E), error = function(e) e)
str(ee)
ee
stopifnot(exprs = {
inherits(ee, "missingArgError") # and
inherits(ee, "getMissingError")
##
inherits(tryCatch(get0("m", E), error=identity), "getMissingError")
is.symbol(E$m) # => valid argument to get(), and *also* gets 'missing arg':
inherits(tryCatch(get ( E$m ) , error=identity), "getMissingError")
})
[Package _base_ version 4.6.0 Index]