Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keep_at documentation says to use vars, but doing this yields a warning that vars is deprecated #1061

Closed
shearerpmm opened this issue Mar 9, 2023 · 3 comments

Comments

@shearerpmm
Copy link

Now that vars is deprecated, what is the right way to write this code?

library(tidyverse)
x <- c(a = 1, b = 2, c = 3, cat = 10, dog = 15, elephant = 5, e = 10)

x %>% keep_at(vars(starts_with('c')))
#> Warning: Using `vars()` in .at was deprecated in purrr 1.0.0.
#>   c cat 
#>   3  10

x %>% keep_at(starts_with('c'))
#> Error:
#> ! `starts_with()` must be used within a *selecting* function.
#> ℹ See <https://tidyselect.r-lib.org/reference/faq-selection-context.html> for
#>   details.

#> Backtrace:
#>     ▆
#>  1. ├─x %>% keep_at(starts_with("c"))
#>  2. ├─purrr::keep_at(., starts_with("c"))
#>  3. │ └─purrr:::where_at(x, at, user_env = caller_env())
#>  4. │   └─rlang::is_formula(at)
#>  5. └─tidyselect::starts_with("c")
#>  6.   ├─vars %||% peek_vars(fn = "starts_with")
#>  7.   └─tidyselect::peek_vars(fn = "starts_with")
#>  8.     └─cli::cli_abort(...)
#>  9.       └─rlang::abort(...)
@haozhou1988
Copy link

using x %>% keep_at(grepl("^c", names(.)))?

@francisbarton
Copy link

using x %>% keep_at(grepl("^c", names(.)))?

you don't need the names() in there because the function in at in keep_at() already operates on the names of the vector.

purrr::keep_at(x, \(x) grepl("^c", x))

@hadley
Copy link
Member

hadley commented Jul 26, 2023

Or purrr::keep_at(x, \(x) startsWith(x, "c")) 😄

@hadley hadley closed this as completed Jul 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants