I wrote an R package that utilizes the {tidyselect} selectors (e.g. contains()
, starts_with()
, etc.). I would like to add a few more select helper functions to the package to select variables based on some attribute. For example, select all numeric variables or perhaps all logical variables.
I have reviewed the {tidyselect} base code. But I can't surmise how the registration of the variables is working, and therefore can't extend it to select variables by their attributes.
I have done some searching, and it looks like the {recipes} package has successfully implemented additional helpers like I am looking for (e.g. all_numeric()
), but I am struggling to write extension functions myself. https://github.com/tidymodels/recipes/blob/master/R/selections.R
What it comes down to, I believe, is that I do not understand what is happening when the variables are registered with the tidyselect::scoped_vars()
function. If I run tidyselect::scoped_vars(vars = names(mtcars))
in a clean environment, I don't see any changed being made. But I am able to use the {tidyselect} helpers in the global environment after registering the variables.
names(mtcars)
#> [1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
#> [11] "carb"
tidyselect::scoped_vars(vars = names(mtcars))
# returns position of column 'mpg'
tidyselect::starts_with("mp")
#> 1
Any tips or direction to some documentation would be GREATLY appreciated! Thank you!