Examples for 'rlang::sym'


Create a symbol or list of symbols

Aliases: sym syms data_sym data_syms

Keywords:

### ** Examples

# Create a symbol
sym("cyl")
cyl
# Create a list of symbols
syms(c("cyl", "am"))
[[1]]
cyl

[[2]]
am
# Symbolised names refer to variables
eval(sym("cyl"), mtcars)
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
# Beware of scoping issues
Cyl <- "wrong"
eval(sym("Cyl"), mtcars)
[1] "wrong"
# Data symbols are explicitly scoped in the data mask
try(eval_tidy(data_sym("Cyl"), mtcars))
Error in .data$Cyl : Column `Cyl` not found in `.data`.
# These can only be used with tidy eval functions
try(eval(data_sym("Cyl"), mtcars))
Error in eval(data_sym("Cyl"), mtcars) : 
  Can't subset `.data` outside of a data mask context.
# The empty string returns the missing argument:
sym("")
# This way sym() and as_string() are inverse of each other:
as_string(missing_arg())
[1] ""
sym(as_string(missing_arg()))

[Package rlang version 1.1.4 Index]