Is current_vars() meant for general use or is there a shorthand?

Assuming I can't use matches() because of this dplyr issue, is this the way I am meant to use select to filter columns by some test on the column name?

select(test, str_subset(current_vars(), "somePattern"))

Specifically, is current_vars for general use? It's listed in the help alongside matches, starts_with etc. but there's no specific documentation of it. I've just used it because it seems to be what the other functions like matches use.

If you are using these functions in a package, you should consider the versions in tidyselect instead. The function names are a little different but this package is meant for more general usage.

Are they just duplicates then with the idea that they would get deprecated in dplyr? Or do you mean using everything() instead of current_vars()?

It is for a package, but the pattern will be hardcoded, I'm not needing to give the package consumer any control over anything really other than the tibble that is being filtered.

Yes current_vars() is meant to create tidyselect helpers and users are encouraged to do so. The next version of dplyr will start to use tidyselect but current_vars() will remain around. We suggest you use tidyselect::peek_vars() once the next dplyr version is out though.

If you're using a selection pattern often it may be nice to extract it in a function:

vars_subset <- function(pattern) {
  str_subset(tidyselect::peek_vars(), pattern)

It will work in all selecting functions like dplyr::select() but also tidyr::gather() etc.

1 Like

Thanks, that's useful insight. I'll keep an eye out for the new versions. I did find it a bit confusing to know whether to use select_at or select and I still haven't quite understood the difference. Hopefully matches() can get updated to use stringr anyway and then my function will not be required anymore.

select() and select_at() are mostly equivalent. The purpose of the latter is mainly to make select_ a complete family. The main difference is that you can pass additional functions to modify the names:

select_at(mtcars, vars(matches("sp"), toupper)
#> # A tibble: 32 x 1
#>     DISP
#>  * <dbl>
#>  1   160
#>  2   160
#>  3   108
#>  4   258
#>  5   360
#>  6   225
#>  7   360
#>  8   147
#>  9   141
#> 10   168
#> # ... with 22 more rows
1 Like

It seems that current_vars() may no longer work in select(data, str_subset(current_vars(), pattern)) so I have switched to peek_vars() which works fine.

Not sure what changed as I updated tidyverse but I don't think there was a dplyr update in there. It's possible it was something funny that I did.