I want to string of a column name to dplyr::arrange
in a form am <- "cyl"
. The purpose is to sort by column cyl
.
dplyr::arrange(mtcars, cyl)
am <- "cyl"
rlang::quo
dplyr::arrange(mtcars, !!rlang::quo(am))
Sorted by am
not cyl
.
rang::ensym
dplyr::arrange(mtcars, !!rlang::ensym(am))
Sorted by am
not cyl
.
dplyr::arrange(mtcars, {{am}})
Not sorted.
In actual function I'm sorting data frame by index column I'm creating. The variable with column name is called index_column
. I want protect myself from, albeit highly unlikely, scenario of actual data containing index_column
. I could solve that using make.names
and scanning for unique column names but I'm more interested in solving the problem above.
It would be sym
out2 <- dplyr::arrange(mtcars, !!rlang::sym(am))
-testing with OP's expected
out1 <- dplyr::arrange(mtcars, cyl)
identical(out1, out2)
#[1] TRUE