Last active
May 28, 2022 03:22
-
-
Save moodymudskipper/8d2e0eb6afd73f4d3206e8cd8ae161a5 to your computer and use it in GitHub Desktop.
sort tidy selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abc <- function(..., desc = FALSE) { | |
data <- tidyselect::peek_data() | |
named_selection <- tidyselect::eval_select(rlang::expr(c(...)), data) | |
named_selection[order(names(named_selection), named_selection, decreasing = desc)] | |
} | |
library(dplyr, w = F) | |
mtcars %>% | |
as_tibble() %>% | |
select( | |
wt, | |
abc(starts_with("c"), contains("a")), | |
abc(contains("s"), desc = TRUE), | |
everything() | |
) | |
#> # A tibble: 32 × 11 | |
#> wt am carb cyl drat gear vs qsec disp mpg hp | |
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> | |
#> 1 2.62 1 4 6 3.9 4 0 16.5 160 21 110 | |
#> 2 2.88 1 4 6 3.9 4 0 17.0 160 21 110 | |
#> 3 2.32 1 1 4 3.85 4 1 18.6 108 22.8 93 | |
#> 4 3.22 0 1 6 3.08 3 1 19.4 258 21.4 110 | |
#> 5 3.44 0 2 8 3.15 3 0 17.0 360 18.7 175 | |
#> 6 3.46 0 1 6 2.76 3 1 20.2 225 18.1 105 | |
#> 7 3.57 0 4 8 3.21 3 0 15.8 360 14.3 245 | |
#> 8 3.19 0 2 4 3.69 4 1 20 147. 24.4 62 | |
#> 9 3.15 0 2 4 3.92 4 1 22.9 141. 22.8 95 | |
#> 10 3.44 0 4 6 3.92 4 1 18.3 168. 19.2 123 | |
#> # … with 22 more rows |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment