tl; dr: df %>% arrange(desc(!!sym("b")))
First of all, standard verbs of dplyr verbs dplyr out of date, so instead of:
library(dplyr) x <- "b" df %>% arrange_(x)
Now it is recommended to type:
library(dplyr) library(rlang) df %>% arrange(!!sym(x))
See ?arrange_ , this is a link to the ?arrange_ help" called Deprecated SE versions of main verbs. and offers some details.
From there, sorting in descending order it is easy to adapt the new wording:
df %>% arrange(desc(!!sym(x)))
This also works if your data is not grouped:
df %>% arrange(desc(.[[x]])) df %>% arrange(desc(.data[[x]]))
For your information, to make arrange_ work arrange_ we could do the following, but itβs better to use the approach described above!
df %>% arrange_(paste0("desc(",x,")"))
Which can be simplified if we have numerical variables, as in the example with OP:
df %>% arrange_(paste0("-",x))
Or using lazyeval::interp
df %>% arrange_(interp(~desc(y),y=as.name(x)))
Or as @ shyam-saladi suggests:
desc_ <- function(x) lazyeval::interp(~desc(var), var = as.name(x))