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)

Arguments

...

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).

coerce

If TRUE, the returned value is coerced to the class of the default value (unless NULL) using as.

envir

A environment or a named list where to look for the variable. Only if envir is an environment.

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 if envir is an environment.

cmdArg

If TRUE, the corresponding command-line argument is used as the default value.

Value

Returns an object.

Details

ecget(...) is short for eget(..., cmdArg=TRUE).

Author

Henrik Bengtsson

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
#> [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