eget.Rd
Gets a variable by name. If non-existing, the default value is returned.
eget(..., coerce=TRUE, envir=parent.frame(), inherits=FALSE, mode="default",
cmdArg=FALSE)
Named arguments name
and default
, where
name
must be a character
string and default
is
an optional default value (if not given, it's NULL
).
Alternatively, name
and default
can be given as
a named argument (e.g. n=42
).
If TRUE
, the returned value is coerced to the class
of the default value (unless NULL
) using as
.
A environment
or a named list
where to look
for the variable. Only if envir
is an environment
.
A logical
specifying whether the enclosing frames
of the environment should be searched or not.
A character
string specifying the mode of the object to
retrieve. Only if envir
is an environment
.
If TRUE
, the corresponding command-line argument
is used as the default value.
Returns an object.
ecget(...)
is short for eget(..., cmdArg=TRUE)
.
# Get variable 'a' if it exists, otherwise return the default value.
value <- eget("a", default=42L)
print(value) # 42L
#> [1] 42
# Short version doing the same
value <- eget(a=42L)
print(value) # 42L
#> [1] 42
# Same, but look for the variable in 'envir' (here a list)
value <- eget("a", default=42L, envir=list(a=1))
print(value) # 1L
#> [1] 1
# 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)
#> [1] 42
# Equivalently.
value <- ecget(n=42L)
print(value)
#> [1] 42