Skip to content

Instantly share code, notes, and snippets.

@moodymudskipper
Last active May 28, 2022 03:22

Revisions

  1. moodymudskipper revised this gist May 6, 2022. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions abc.R
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,6 @@ mtcars %>%
    select(
    wt,
    abc(starts_with("c"), contains("a")),
    abc(starts_with("c"), contains("a")),
    abc(contains("s"), desc = TRUE),
    everything()
    )
    @@ -27,4 +26,4 @@ mtcars %>%
    #> 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
    #> # … with 22 more rows
  2. moodymudskipper created this gist May 6, 2022.
    30 changes: 30 additions & 0 deletions abc.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    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(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