| 1 |
#' Select variables that match a pattern |
| 2 |
#' |
| 3 |
#' @description |
| 4 |
#' |
| 5 |
#' These [selection helpers][language] match variables according |
| 6 |
#' to a given pattern. |
| 7 |
#' |
| 8 |
#' * [starts_with()]: Starts with an exact prefix. |
| 9 |
#' * [ends_with()]: Ends with an exact suffix. |
| 10 |
#' * [contains()]: Contains a literal string. |
| 11 |
#' * [matches()]: Matches a regular expression. |
| 12 |
#' * [num_range()]: Matches a numerical range like x01, x02, x03. |
| 13 |
#' |
| 14 |
#' @param match A character vector. If length > 1, the union of the |
| 15 |
#' matches is taken. |
| 16 |
#' |
| 17 |
#' For `starts_with()`, `ends_with()`, and `contains()` this is an exact |
| 18 |
#' match. For `matches()` this is a regular expression, and can be a |
| 19 |
#' stringr pattern. |
| 20 |
#' @param ignore.case If `TRUE`, the default, ignores case when matching |
| 21 |
#' names. |
| 22 |
#' @param vars A character vector of variable names. If not supplied, |
| 23 |
#' the variables are taken from the current selection context (as |
| 24 |
#' established by functions like `select()` or `pivot_longer()`). |
| 25 |
#' |
| 26 |
#' @section Examples: |
| 27 |
#' |
| 28 |
#' ```{r, child = "man/rmd/setup.Rmd"} |
| 29 |
#' ``` |
| 30 |
#' |
| 31 |
#' Selection helpers can be used in functions like `dplyr::select()` |
| 32 |
#' or `tidyr::pivot_longer()`. Let's first attach the tidyverse: |
| 33 |
#' |
| 34 |
#' ```{r, comment = "#>", collapse = TRUE} |
| 35 |
#' library(tidyverse) |
| 36 |
#' |
| 37 |
#' # For better printing |
| 38 |
#' iris <- as_tibble(iris) |
| 39 |
#' ``` |
| 40 |
#' |
| 41 |
#' `starts_with()` selects all variables matching a prefix and |
| 42 |
#' `ends_with()` matches a suffix: |
| 43 |
#' |
| 44 |
#' ```{r, comment = "#>", collapse = TRUE} |
| 45 |
#' iris %>% select(starts_with("Sepal")) |
| 46 |
#' |
| 47 |
#' iris %>% select(ends_with("Width")) |
| 48 |
#' ``` |
| 49 |
#' |
| 50 |
#' You can supply multiple prefixes or suffixes. Note how the order of |
| 51 |
#' variables depends on the order of the suffixes and prefixes: |
| 52 |
#' |
| 53 |
#' ```{r, comment = "#>", collapse = TRUE} |
| 54 |
#' iris %>% select(starts_with(c("Petal", "Sepal"))) |
| 55 |
#' |
| 56 |
#' iris %>% select(ends_with(c("Width", "Length"))) |
| 57 |
#' ``` |
| 58 |
#' |
| 59 |
#' `contains()` selects columns whose names contain a word: |
| 60 |
#' |
| 61 |
#' ```{r, comment = "#>", collapse = TRUE} |
| 62 |
#' iris %>% select(contains("al")) |
| 63 |
#' ``` |
| 64 |
#' |
| 65 |
#' `starts_with()`, `ends_with()`, and `contains()` do not use regular expressions. To select with a |
| 66 |
#' regexp use `matches()`: |
| 67 |
#' |
| 68 |
#' ```{r, comment = "#>", collapse = TRUE} |
| 69 |
#' # [pt] is matched literally: |
| 70 |
#' iris %>% select(contains("[pt]al")) |
| 71 |
#' |
| 72 |
#' # [pt] is interpreted as a regular expression |
| 73 |
#' iris %>% select(matches("[pt]al")) |
| 74 |
#' ``` |
| 75 |
#' |
| 76 |
#' `starts_with()` selects all variables starting with a prefix. To |
| 77 |
#' select a range, use `num_range()`. Compare: |
| 78 |
#' |
| 79 |
#' ```{r, comment = "#>", collapse = TRUE} |
| 80 |
#' billboard %>% select(starts_with("wk")) |
| 81 |
#' |
| 82 |
#' billboard %>% select(num_range("wk", 10:15)) |
| 83 |
#' ``` |
| 84 |
#' |
| 85 |
#' @seealso `r rd_helpers_seealso()` |
| 86 |
#' @export |
| 87 |
starts_with <- function(match, |
| 88 |
ignore.case = TRUE, |
| 89 |
vars = NULL) { |
| 90 |
check_match(match) |
| 91 |
vars <- vars %||% peek_vars(fn = "starts_with") |
| 92 |
|
| 93 |
if (ignore.case) { |
| 94 |
vars <- tolower(vars) |
| 95 |
match <- tolower(match) |
| 96 |
} |
| 97 |
|
| 98 |
flat_map_int(match, starts_with_impl, vars) |
| 99 |
} |
| 100 |
starts_with_impl <- function(x, vars) { |
| 101 |
n <- nchar(x) |
| 102 |
which_vars(x, substr(vars, 1, n)) |
| 103 |
} |
| 104 |
|
| 105 |
#' @rdname starts_with |
| 106 |
#' @export |
| 107 |
ends_with <- function(match, |
| 108 |
ignore.case = TRUE, |
| 109 |
vars = NULL) { |
| 110 |
check_match(match) |
| 111 |
vars <- vars %||% peek_vars(fn = "ends_with") |
| 112 |
|
| 113 |
if (ignore.case) { |
| 114 |
vars <- tolower(vars) |
| 115 |
match <- tolower(match) |
| 116 |
} |
| 117 |
|
| 118 |
length <- nchar(vars) |
| 119 |
flat_map_int(match, ends_with_impl, vars, length) |
| 120 |
} |
| 121 |
ends_with_impl <- function(x, vars, length) { |
| 122 |
n <- nchar(x) |
| 123 |
which_vars(x, substr(vars, pmax(1, length - n + 1), length)) |
| 124 |
} |
| 125 |
|
| 126 |
#' @rdname starts_with |
| 127 |
#' @export |
| 128 |
contains <- function(match, |
| 129 |
ignore.case = TRUE, |
| 130 |
vars = NULL) { |
| 131 |
check_match(match) |
| 132 |
vars <- vars %||% peek_vars(fn = "contains") |
| 133 |
|
| 134 |
if (ignore.case) { |
| 135 |
vars <- tolower(vars) |
| 136 |
match <- tolower(match) |
| 137 |
} |
| 138 |
|
| 139 |
flat_map_int(match, grep_vars, vars, fixed = TRUE) |
| 140 |
} |
| 141 |
|
| 142 |
#' @rdname starts_with |
| 143 |
#' @param perl Should Perl-compatible regexps be used? |
| 144 |
#' @export |
| 145 |
matches <- function(match, |
| 146 |
ignore.case = TRUE, |
| 147 |
perl = FALSE, |
| 148 |
vars = NULL) { |
| 149 |
check_match(match) |
| 150 |
vars <- vars %||% peek_vars(fn = "matches") |
| 151 |
|
| 152 |
if (inherits(match, "pattern") || inherits(match, "stringr_pattern")) { |
| 153 |
check_installed("stringr") |
| 154 |
if (!missing(ignore.case)) { |
| 155 |
cli::cli_abort("{.arg ignore.case} not supported when {.arg match} is a {.pkg stringr} pattern.") |
| 156 |
} |
| 157 |
if (!missing(perl)) { |
| 158 |
cli::cli_abort("{.arg perl} not supported when {.arg match} is a {.pkg stringr} pattern.") |
| 159 |
} |
| 160 |
|
| 161 |
# no [ or [[ methods for pattern objects |
| 162 |
if (length(match) > 1) { |
| 163 |
cli::cli_abort("{.pkg stringr} patterns must be length 1.") |
| 164 |
} |
| 165 |
|
| 166 |
stringr::str_which(vars, match) |
| 167 |
} else { |
| 168 |
flat_map_int(match, grep_vars, vars, ignore.case = ignore.case, perl = perl) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
#' @rdname starts_with |
| 173 |
#' @param prefix,suffix A prefix/suffix added before/after the numeric range. |
| 174 |
#' @param range A sequence of integers, like `1:5`. |
| 175 |
#' @param width Optionally, the "width" of the numeric range. For example, |
| 176 |
#' a range of 2 gives "01", a range of three "001", etc. |
| 177 |
#' @export |
| 178 |
num_range <- function(prefix, |
| 179 |
range, |
| 180 |
suffix = "", |
| 181 |
width = NULL, |
| 182 |
vars = NULL) { |
| 183 |
vars <- vars %||% peek_vars(fn = "num_range") |
| 184 |
|
| 185 |
if (!is_null(width)) { |
| 186 |
range <- sprintf(paste0("%0", width, "d"), range) |
| 187 |
} |
| 188 |
|
| 189 |
match_vars(paste0(prefix, range, suffix), vars) |
| 190 |
} |
| 191 |
|
| 192 |
check_match <- function(match) { |
| 193 |
if (!is_character(match) || !all(nzchar(match))) { |
| 194 |
cli::cli_abort("{.arg match} must be a character vector of non empty strings.") |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
match_vars <- function(needle, haystack) { |
| 199 |
if (vctrs::vec_duplicate_any(haystack)) { |
| 200 |
x <- map(needle, ~ which(. == haystack)) |
| 201 |
x <- vctrs::vec_c(!!!x) |
| 202 |
} else { |
| 203 |
x <- match(needle, haystack) |
| 204 |
x[!is.na(x)] |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
grep_vars <- function(needle, haystack, ...) { |
| 209 |
grep(needle, haystack, ...) |
| 210 |
} |
| 211 |
|
| 212 |
which_vars <- function(needle, haystack) { |
| 213 |
which(needle == haystack) |
| 214 |
} |