Debian Codegraph @

R/helpers-vector.R

raw
1 #' Select variables from character vectors
2 #'
3 #' @description
4 #'
5 #' These [selection helpers][language] select variables
6 #' contained in a character vector. They are especially useful for
7 #' programming with selecting functions.
8 #'
9 #' * [all_of()] is for strict selection. If any of the variables in
10 #' the character vector is missing, an error is thrown.
11 #'
12 #' * [any_of()] doesn't check for missing variables. It is especially
13 #' useful with negative selections, when you would like to make sure
14 #' a variable is removed.
15 #'
16 #' The order of selected columns is determined by the order in the
17 #' vector.
18 #'
19 #' @inheritParams starts_with
20 #' @param x A vector of character names or numeric locations.
21 #'
22 #' @section Examples:
23 #'
24 #' ```{r, child = "man/rmd/setup.Rmd"}
25 #' ```
26 #'
27 #' Selection helpers can be used in functions like `dplyr::select()`
28 #' or `tidyr::pivot_longer()`. Let's first attach the tidyverse:
29 #'
30 #' ```{r, comment = "#>", collapse = TRUE}
31 #' library(tidyverse)
32 #'
33 #' # For better printing
34 #' iris <- as_tibble(iris)
35 #' ```
36 #'
37 #' It is a common to have a names of variables in a vector.
38 #'
39 #' ```{r, comment = "#>", collapse = TRUE}
40 #' vars <- c("Sepal.Length", "Sepal.Width")
41 #'
42 #' iris[, vars]
43 #' ```
44 #'
45 #' To refer to these variables in selecting function, use `all_of()`:
46 #'
47 #' ```{r, comment = "#>", collapse = TRUE}
48 #' iris %>% select(all_of(vars))
49 #'
50 #' iris %>% pivot_longer(all_of(vars))
51 #' ```
52 #'
53 #' If any of the variable is missing from the data frame, that's an error:
54 #'
55 #' ```{r, error = TRUE}
56 #' starwars %>% select(all_of(vars))
57 #' ```
58 #'
59 #' Use `any_of()` to allow missing variables:
60 #'
61 #' ```{r, comment = "#>", collapse = TRUE}
62 #' starwars %>% select(any_of(vars))
63 #' ```
64 #'
65 #' `any_of()` is especially useful to remove variables from a data
66 #' frame because calling it again does not cause an error:
67 #'
68 #' ```{r, comment = "#>", collapse = TRUE}
69 #' iris %>% select(-any_of(vars))
70 #'
71 #' iris %>% select(-any_of(vars)) %>% select(-any_of(vars))
72 #' ```
73 #'
74 #' @seealso `r rd_helpers_seealso()`
75 #' @export
76 all_of <- function(x) {
77 if (!has_vars()) {
78 lifecycle::deprecate_soft(
79 "1.2.0",
80 I("Using `all_of()` outside of a selecting function"),
81 details = paste("See details at", peek_vars_link())
82 )
83 return(x)
84 }
85
86 vars <- peek_vars(fn = "all_of")
87 as_indices_impl(x, vars = vars, strict = TRUE)
88 }
89
90 #' @rdname all_of
91 #' @inheritParams rlang::args_dots_empty
92 #' @export
93 any_of <- function(x, ..., vars = NULL) {
94 vars <- vars %||% peek_vars(fn = "any_of")
95 if (!missing(...)) {
96 cli::cli_abort(c(
97 "{.arg ...} must be empty.",
98 i = "Did you forget {.code c()}?",
99 i = 'The expected syntax is {.code any_of(c("a", "b"))}, not {.code any_of("a", "b")}'
100 ))
101 }
102 as_indices_impl(x, vars = vars, strict = FALSE)
103 }