| 1 | # scalar boolean operators fail informatively |
| 2 | |
| 3 | Code |
| 4 | select_loc(letters2, starts_with("a") || ends_with("b")) |
| 5 | Condition |
| 6 | Error in `select_loc()`: |
| 7 | ! Can't use scalar `||` in selections. |
| 8 | i Do you need `|` instead? |
| 9 | Code |
| 10 | select_loc(letters2, starts_with("a") && ends_with("b")) |
| 11 | Condition |
| 12 | Error in `select_loc()`: |
| 13 | ! Can't use scalar `&&` in selections. |
| 14 | i Do you need `&` instead? |
| 15 | |
| 16 | # can't use `*` and `^` in data context |
| 17 | |
| 18 | Code |
| 19 | select_loc(letters2, a * 2) |
| 20 | Condition |
| 21 | Error in `select_loc()`: |
| 22 | ! Can't use arithmetic operator `*` in selection context. |
| 23 | Code |
| 24 | select_loc(letters2, a^2) |
| 25 | Condition |
| 26 | Error in `select_loc()`: |
| 27 | ! Can't use arithmetic operator `^` in selection context. |
| 28 | |
| 29 | # symbol lookup outside data informs caller about better practice |
| 30 | |
| 31 | Code |
| 32 | vars <- c("a", "b") |
| 33 | select_loc(letters2, vars) |
| 34 | Condition |
| 35 | Warning: |
| 36 | Using an external vector in selections was deprecated in tidyselect 1.1.0. |
| 37 | i Please use `all_of()` or `any_of()` instead. |
| 38 | # Was: |
| 39 | data %>% select(vars) |
| 40 | |
| 41 | # Now: |
| 42 | data %>% select(all_of(vars)) |
| 43 | |
| 44 | See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>. |
| 45 | Output |
| 46 | a b |
| 47 | 1 2 |
| 48 | |
| 49 | # can forbid use of predicates |
| 50 | |
| 51 | Code |
| 52 | select_loc(iris, where(is.factor), allow_predicates = FALSE) |
| 53 | Condition |
| 54 | Error in `select_loc()`: |
| 55 | ! This tidyselect interface doesn't support predicates. |
| 56 | |
| 57 | # selections provide informative errors |
| 58 | |
| 59 | Code |
| 60 | # Foreign errors during evaluation |
| 61 | select_loc(iris, eval_tidy(foobar)) |
| 62 | Condition |
| 63 | Error in `select_loc()`: |
| 64 | i In argument: `eval_tidy(foobar)`. |
| 65 | Caused by error: |
| 66 | ! object 'foobar' not found |
| 67 | |
| 68 | # use of .data is deprecated |
| 69 | |
| 70 | Code |
| 71 | x <- select_loc(x, .data$a) |
| 72 | Condition |
| 73 | Warning: |
| 74 | Use of .data in tidyselect expressions was deprecated in tidyselect 1.2.0. |
| 75 | i Please use `"a"` instead of `.data$a` |
| 76 | |
| 77 | --- |
| 78 | |
| 79 | Code |
| 80 | x <- select_loc(x, .data[[var]]) |
| 81 | Condition |
| 82 | Warning: |
| 83 | Use of .data in tidyselect expressions was deprecated in tidyselect 1.2.0. |
| 84 | i Please use `all_of(var)` (or `any_of(var)`) instead of `.data[[var]]` |
| 85 | |
| 86 | # eval_walk() has informative messages |
| 87 | |
| 88 | Code |
| 89 | # Using a predicate without where() warns |
| 90 | invisible(select_loc(iris, is_integer)) |
| 91 | Condition |
| 92 | Warning: |
| 93 | Use of bare predicate functions was deprecated in tidyselect 1.1.0. |
| 94 | i Please use wrap predicates in `where()` instead. |
| 95 | # Was: |
| 96 | data %>% select(is_integer) |
| 97 | |
| 98 | # Now: |
| 99 | data %>% select(where(is_integer)) |
| 100 | Code |
| 101 | invisible(select_loc(iris, is.numeric)) |
| 102 | Condition |
| 103 | Warning: |
| 104 | Use of bare predicate functions was deprecated in tidyselect 1.1.0. |
| 105 | i Please use wrap predicates in `where()` instead. |
| 106 | # Was: |
| 107 | data %>% select(is.numeric) |
| 108 | |
| 109 | # Now: |
| 110 | data %>% select(where(is.numeric)) |
| 111 | Code |
| 112 | invisible(select_loc(iris, isTRUE)) |
| 113 | Condition |
| 114 | Warning: |
| 115 | Use of bare predicate functions was deprecated in tidyselect 1.1.0. |
| 116 | i Please use wrap predicates in `where()` instead. |
| 117 | # Was: |
| 118 | data %>% select(isTRUE) |
| 119 | |
| 120 | # Now: |
| 121 | data %>% select(where(isTRUE)) |
| 122 | Code |
| 123 | # Warning is not repeated |
| 124 | invisible(select_loc(iris, is_integer)) |
| 125 | Condition |
| 126 | Warning: |
| 127 | Use of bare predicate functions was deprecated in tidyselect 1.1.0. |
| 128 | i Please use wrap predicates in `where()` instead. |
| 129 | # Was: |
| 130 | data %>% select(is_integer) |
| 131 | |
| 132 | # Now: |
| 133 | data %>% select(where(is_integer)) |
| 134 | Code |
| 135 | # formula shorthand must be wrapped |
| 136 | (expect_error(select_loc(mtcars, ~ is.numeric(.x)))) |
| 137 | Output |
| 138 | <error/rlang_error> |
| 139 | Error in `select_loc()`: |
| 140 | ! Formula shorthand must be wrapped in `where()`. |
| 141 | |
| 142 | # Bad |
| 143 | data %>% select(~is.numeric(.x)) |
| 144 | |
| 145 | # Good |
| 146 | data %>% select(where(~is.numeric(.x))) |
| 147 | Code |
| 148 | (expect_error(select_loc(mtcars, ~ is.numeric(.x) || is.factor(.x) || |
| 149 | is.character(.x)))) |
| 150 | Output |
| 151 | <error/rlang_error> |
| 152 | Error in `select_loc()`: |
| 153 | ! Formula shorthand must be wrapped in `where()`. |
| 154 | |
| 155 | # Bad |
| 156 | data %>% select(~is.numeric(.x) || is.factor(.x) || is.character(.x)) |
| 157 | |
| 158 | # Good |
| 159 | data %>% select(where(~is.numeric(.x) || is.factor(.x) || is.character(.x))) |
| 160 | Code |
| 161 | (expect_error(select_loc(mtcars, ~ is.numeric(.x) || is.factor(.x) || |
| 162 | is.character(.x) || is.numeric(.x) || is.factor(.x) || is.character(.x)))) |
| 163 | Output |
| 164 | <error/rlang_error> |
| 165 | Error in `select_loc()`: |
| 166 | ! Formula shorthand must be wrapped in `where()`. |
| 167 | |
| 168 | # Bad |
| 169 | data %>% select(~...) |
| 170 | |
| 171 | # Good |
| 172 | data %>% select(where(~...)) |
| 173 | Code |
| 174 | (expect_error(select_loc(mtcars, .data$"foo"))) |
| 175 | Output |
| 176 | <error/rlang_error> |
| 177 | Error in `select_loc()`: |
| 178 | ! The RHS of `.data$rhs` must be a symbol. |
| 179 | |
| 180 | # can forbid empty selection |
| 181 | |
| 182 | Code |
| 183 | ensure_named(integer(), allow_empty = FALSE) |
| 184 | Condition |
| 185 | Error: |
| 186 | ! Must select at least one item. |
| 187 | Code |
| 188 | ensure_named(integer(), allow_empty = FALSE, allow_rename = FALSE) |
| 189 | Condition |
| 190 | Error: |
| 191 | ! Must select at least one item. |
| 192 |