eget
From R.utils v1.26.2
by Henrik Bengtsson
Gets a variable by name
Gets a variable by name. If non-existing, the default value is returned.
Usage
eget(..., coerce=TRUE, envir=parent.frame(), inherits=TRUE, mode="any", cmdArg=FALSE)
Arguments
- ...
- Named arguments
name
anddefault
, wherename
must be acharacter
string anddefault
is an optional default value (if not given, it's<
- coerce
- If
TRUE
, the returned value is coerced to the class of the default value (unlessNULL
) usingas
- envir
- A
environment
or a namedlist
where to look for the variable. Only ifenvir
is anen
- inherits
- A
logical
specifying whether the enclosing frames of the environment should be searched or not. - mode
- A
character
string specifying the mode of the object to retrieve. Only ifenvir
is anenvironment
. - cmdArg
- If
TRUE
, the corresponding command-line argument is used as the default value.
Details
ecget(...)
is short for eget(..., cmdArg=TRUE)
.
Value
- Returns an object.
See Also
To retrieve command-line arguments, see cmdArg
.
See also mget
().
Examples
# Get variable 'a' if it exists, otherwise return the default value.
value <- eget("a", default=42L)
print(value) # 42L
# Short version doing the same
value <- eget(a=42L)
print(value) # 42L
# Same, but look for the variable in 'envir' (here a list)
value <- eget("a", default=42L, envir=list(a=1))
print(value) # 1L
# Get variable 'n', which defaults to command-line argument
# 'n' ('-n' or '--n'), which in turn defaults to 42L.
value <- eget(n=cmdArg(n=42L))
print(value)
# Equivalently.
value <- ecget(n=42L)
print(value)
Community examples
Looks like there are no examples yet.