Learn R Programming

R.utils (version 2.12.3)

tempvar: Gets a unique non-existing temporary variable name

Description

Gets a unique non-existing temporary variable name, and optionally assigns it an initial value.

Usage

tempvar(prefix="var", value, envir=parent.frame(), inherits=FALSE)

Value

Returns a character string.

Arguments

prefix

A character string specifying the prefix of the temporary variable name.

value

(optional) If given, a variable with the temporary name is assigned this value. Only works if envir is an environment.

envir

An environment, a named list, or a named data.frame, whose elements the temporary variable should not clash with.

inherits

A logical specifying whether the enclosing frames of the environment should be searched or not.

Author

Henrik Bengtsson

See Also

tempfile() and assign().

Examples

Run this code
# Get a temporary variable
name <- tempvar()
print(name)

# Get and assign a temporary variable
name <- tempvar(value=base::letters)
print(name)
str(get(name))

# Get a temporary variable with custom prefix
name <- tempvar(prefix=".hidden")
print(name)

# Get a temporary variable for a data.frame
name <- tempvar(envir = datasets::mtcars)
print(name)

Run the code above in your browser using DataLab