Skip to contents

Used within a function this allows for a list of columns to be given as a parameter to the parent function in a number of flexible ways. A list of unquoted symbols, a list of quoted strings, a tidyselect syntax (assuming the parent function has a dataframe is its first argument) or as a formula.

Usage

ensyms2(
  x,
  .as = c("symbol", "character"),
  .side = c("rhs", "lhs"),
  .tidy = FALSE
)

Arguments

x

one of a list of symbols, a list of strings, a tidyselect expression, or a formula

.as

the type of output desired: (symbol or character)

.side

the desired side of formulae output: (lhs or rhs); this is only relevant if x is a formula (or list of formulae)

.tidy

is this being called in the context of a "tidy" style function. I.e. one that takes a dataframe as the main parameter? (Default is FALSE)

Value

either a list of symbols or a character vector of the symbols

Examples


# TODO: convert these to tests
eg = function(df, vars, ...) {
  vars = ensyms2(vars, ..., .tidy=TRUE)
  print(vars)
}

eg(iris, c(Sepal.Width, Species, Sepal.Length))
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Species
#> 
#> [[3]]
#> Sepal.Length
#> 
eg(iris, c("Sepal.Width", "Species", "Sepal.Length", "extra"))
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Species
#> 
#> [[3]]
#> Sepal.Length
#> 
#> [[4]]
#> extra
#> 
eg(iris, "Sepal.Width")
#> [[1]]
#> Sepal.Width
#> 
eg(iris, Sepal.Width)
#> [[1]]
#> Sepal.Width
#> 
eg(iris, dplyr::vars(Sepal.Width))
#> [[1]]
#> Sepal.Width
#> 
eg(iris, dplyr::vars(Sepal.Width, Species, Sepal.Length))
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Species
#> 
#> [[3]]
#> Sepal.Length
#> 
eg(iris, list(Sepal.Width, Species, Sepal.Length))
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Species
#> 
#> [[3]]
#> Sepal.Length
#> 
eg(iris, list("Sepal.Width", "Species", "Sepal.Length"))
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Species
#> 
#> [[3]]
#> Sepal.Length
#> 
eg(iris, tidyselect::starts_with("Sepal"))
#> [[1]]
#> Sepal.Length
#> 
#> [[2]]
#> Sepal.Width
#> 
eg(iris, Species ~ Sepal.Width + Sepal.Length)
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Sepal.Length
#> 
eg(iris, Species ~ Sepal.Width + Sepal.Length, .side = "lhs")
#> [[1]]
#> Species
#> 
eg(iris, . ~ Sepal.Width + Sepal.Length, .side = "lhs")
#> [[1]]
#> Petal.Length
#> 
#> [[2]]
#> Petal.Width
#> 
#> [[3]]
#> Species
#> 
eg(iris, Sepal.Width + Sepal.Length ~ .)
#> [[1]]
#> Petal.Length
#> 
#> [[2]]
#> Petal.Width
#> 
#> [[3]]
#> Species
#> 
eg(iris, c(~ Sepal.Width + Sepal.Length, ~ Petal.Width + Petal.Length))
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Sepal.Length
#> 
#> [[3]]
#> Petal.Width
#> 
#> [[4]]
#> Petal.Length
#> 

try(eg(iris, c(~ .)))
#> [[1]]
#> Sepal.Length
#> 
#> [[2]]
#> Sepal.Width
#> 
#> [[3]]
#> Petal.Length
#> 
#> [[4]]
#> Petal.Width
#> 
#> [[5]]
#> Species
#> 
eg(iris, list(~ Sepal.Width + Sepal.Length, ~ Petal.Width + Petal.Length))
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Sepal.Length
#> 
#> [[3]]
#> Petal.Width
#> 
#> [[4]]
#> Petal.Length
#> 

# In a way this shouldn't work, but does:
eg(iris, c(~ Sepal.Width + Sepal.Length, Petal.Width + Petal.Length))
#> [[1]]
#> Sepal.Width
#> 
#> [[2]]
#> Sepal.Length
#> 
#> [[3]]
#> Petal.Width
#> 
#> [[4]]
#> Petal.Length
#> 

# injection support:
subs = ensyms2(c("Sepal.Width", "Species", "Sepal.Length"))

# this must be injected as a single thing as the parameter x but actually it 
# turns out to be just the same as supplying a list of symbols as the bare 
# parameter
# eg(iris,!!subs)
# ensyms2(!!subs)
# same as:
# eg(iris,subs)
# ensyms2(subs)