Debian Codegraph @

R/helpers.R

raw
1 #' Selection language
2 #'
3 #' @description
4 #'
5 #' ## Overview of selection features:
6 #'
7 #' ```{r, child = identity("man/rmd/overview.Rmd")}
8 #' ```
9 #'
10 #' @section Simple examples:
11 #'
12 #' ```{r, child = "man/rmd/setup.Rmd"}
13 #' ```
14 #'
15 #' Here we show the usage for the basic selection operators. See the
16 #' specific help pages to learn about helpers like [starts_with()].
17 #'
18 #' The selection language can be used in functions like
19 #' `dplyr::select()` or `tidyr::pivot_longer()`. Let's first attach
20 #' the tidyverse:
21 #'
22 #' ```{r, comment = "#>", collapse = TRUE}
23 #' library(tidyverse)
24 #'
25 #' # For better printing
26 #' iris <- as_tibble(iris)
27 #' ```
28 #'
29 #' Select variables by name:
30 #'
31 #' ```{r, comment = "#>", collapse = TRUE}
32 #' starwars %>% select(height)
33 #'
34 #' iris %>% pivot_longer(Sepal.Length)
35 #' ```
36 #'
37 #' Select multiple variables by separating them with commas. Note how
38 #' the order of columns is determined by the order of inputs:
39 #'
40 #' ```{r, comment = "#>", collapse = TRUE}
41 #' starwars %>% select(homeworld, height, mass)
42 #' ```
43 #'
44 #' Functions like `tidyr::pivot_longer()` don't take variables with
45 #' dots. In this case use `c()` to select multiple variables:
46 #'
47 #' ```{r, comment = "#>", collapse = TRUE}
48 #' iris %>% pivot_longer(c(Sepal.Length, Petal.Length))
49 #' ```
50 #'
51 #' ## Operators:
52 #'
53 #' The `:` operator selects a range of consecutive variables:
54 #'
55 #' ```{r, comment = "#>", collapse = TRUE}
56 #' starwars %>% select(name:mass)
57 #' ```
58 #'
59 #' The `!` operator negates a selection:
60 #'
61 #' ```{r, comment = "#>", collapse = TRUE}
62 #' starwars %>% select(!(name:mass))
63 #'
64 #' iris %>% select(!c(Sepal.Length, Petal.Length))
65 #'
66 #' iris %>% select(!ends_with("Width"))
67 #' ```
68 #'
69 #' `&` and `|` take the intersection or the union of two selections:
70 #'
71 #' ```{r, comment = "#>", collapse = TRUE}
72 #' iris %>% select(starts_with("Petal") & ends_with("Width"))
73 #'
74 #' iris %>% select(starts_with("Petal") | ends_with("Width"))
75 #' ```
76 #'
77 #' To take the difference between two selections, combine the `&` and
78 #' `!` operators:
79 #'
80 #' ```{r, comment = "#>", collapse = TRUE}
81 #' iris %>% select(starts_with("Petal") & !ends_with("Width"))
82 #' ```
83 #'
84 #' @section Details:
85 #' The order of selected columns is determined by the inputs.
86 #'
87 #' * `all_of(c("foo", "bar"))` selects `"foo"` first.
88 #'
89 #' * `c(starts_with("c"), starts_with("d"))` selects all columns
90 #' starting with `"c"` first, then all columns starting with `"d"`.
91 #'
92 #' @name language
93 #' @aliases select_helpers
94 NULL
95
96
97 #' Superseded selection helpers
98 #'
99 #' `one_of()` is superseded in favour of the more precise [any_of()] and
100 #' [all_of()] selectors.
101 #'
102 #' @param ... One or more character vectors.
103 #' @param .vars A character vector of variable names. When called
104 #' from inside selecting functions like [dplyr::select()] these are
105 #' automatically set to the names of the table.
106 #'
107 #' @keywords internal
108 #' @export
109 one_of <- function(..., .vars = NULL) {
110 .vars <- .vars %||% peek_vars(fn = "one_of")
111 keep <- compact(list(...))
112
113 bad_input <- detect_index(keep, ~ !vec_is_coercible(., chr()))
114 if (bad_input) {
115 type <- obj_type_friendly(keep[[bad_input]])
116 cli::cli_abort(
117 "Input {bad_input} must be a vector of column names, not {type}.",
118 class = "vctrs_error_incompatible_index_type"
119 )
120 }
121
122 keep <- vctrs::vec_c(!!!keep, .ptype = character())
123
124 if (!all(keep %in% .vars)) {
125 bad <- setdiff(keep, .vars)
126 warn(glue("Unknown columns: ", paste0("`", bad, "`", collapse = ", ")))
127 }
128
129 match_vars(keep, .vars)
130 }
131
132 rd_helpers_seealso <- function() {
133 "The [selection language][language] page, which includes links to other selection helpers."
134 }