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

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

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.

Value

Returns a character string.

Author

Henrik Bengtsson

See also

tempfile() and assign().

Examples

# Get a temporary variable
name <- tempvar()
print(name)
#> [1] "var1481152010"

# Get and assign a temporary variable
name <- tempvar(value=base::letters)
print(name)
#> [1] "var1759378318"
str(get(name))
#>  chr [1:26] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" ...

# Get a temporary variable with custom prefix
name <- tempvar(prefix=".hidden")
print(name)
#> [1] ".hidden699884603"

# Get a temporary variable for a data.frame
name <- tempvar(envir = datasets::mtcars)
print(name)
#> [1] "var1163714524"