Learn R Programming

R.utils (version 2.9.0)

eget: Gets a variable by name

Description

Gets a variable by name. If non-existing, the default value is returned.

Usage

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

See Also

To retrieve command-line arguments, see cmdArg. See also mget().

Examples

Run this code
# NOT RUN {
# 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)
# }

Run the code above in your browser using DataLab