Learn R Programming

rlang (version 1.1.3)

sym: Create a symbol or list of symbols

Description

Symbols are a kind of defused expression that represent objects in environments.

  • sym() and syms() take strings as input and turn them into symbols.

  • data_sym() and data_syms() create calls of the form .data$foo instead of symbols. Subsetting the .data pronoun is more robust when you expect a data-variable. See The data mask ambiguity.

Only tidy eval APIs support the .data pronoun. With base R functions, use simple symbols created with sym() or syms().

Usage

sym(x)

syms(x)

data_sym(x)

data_syms(x)

Value

For sym() and syms(), a symbol or list of symbols. For data_sym() and data_syms(), calls of the form .data$foo.

Arguments

x

For sym() and data_sym(), a string. For syms() and data_syms(), a list of strings.

See Also

  • Defusing R expressions

  • Metaprogramming patterns

Examples

Run this code
# Create a symbol
sym("cyl")

# Create a list of symbols
syms(c("cyl", "am"))

# Symbolised names refer to variables
eval(sym("cyl"), mtcars)

# Beware of scoping issues
Cyl <- "wrong"
eval(sym("Cyl"), mtcars)

# Data symbols are explicitly scoped in the data mask
try(eval_tidy(data_sym("Cyl"), mtcars))

# These can only be used with tidy eval functions
try(eval(data_sym("Cyl"), mtcars))

# The empty string returns the missing argument:
sym("")

# This way sym() and as_string() are inverse of each other:
as_string(missing_arg())
sym(as_string(missing_arg()))

Run the code above in your browser using DataLab