Skip to content

Commit

Permalink
Merge pull request #32 from lionel-/impl-helpers-list
Browse files Browse the repository at this point in the history
Export helpers list
  • Loading branch information
lionel- authored Aug 30, 2017
2 parents b1f03f4 + 0d7351f commit d3e2ad7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export(starts_with)
export(vars_pull)
export(vars_rename)
export(vars_select)
export(vars_select_helpers)
export(with_vars)
import(rlang)
importFrom(Rcpp,cppFunction)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ vars_select(names(mtcars), .data$cyl : .data$drat)
* `has_vars()` is a predicate that tests whether a variable context
has been set (#21).

* The selection helpers are now exported in a list
`vars_select_helpers`. This is intended for APIs that embed the
helpers in the evaluation environment.


## Fixes

Expand Down
38 changes: 38 additions & 0 deletions R/select-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,41 @@ grep_vars <- function(needle, haystack, ...) {
which_vars <- function(needle, haystack) {
which(needle == haystack)
}


#' List of selection helpers
#'
#' This list contains all selection helpers exported in tidyselect. It
#' is useful when you want to embed the helpers in your API without
#' having to track addition of new helpers in tidyselect.
#'
#' @export
#' @examples
#' # You can easily embed the helpers by burying them in the scopes of
#' # input quosures. For this example we need an environment where
#' # tidyselect is not attached:
#' local(envir = baseenv(), {
#' vars <- c("foo", "bar", "baz")
#' helpers <- tidyselect::vars_select_helpers
#'
#' my_select <- function(...) {
#' quos <- rlang::quos(...)
#' quos <- lapply(quos, rlang::env_bury, !!! helpers)
#'
#' tidyselect::vars_select(vars, !!! quos)
#' }
#'
#' # The user can now call my_select() with helpers without having
#' # to attach tidyselect:
#' my_select(starts_with("b"))
#' })
vars_select_helpers <- list(
starts_with = starts_with,
ends_with = ends_with,
contains = contains,
matches = matches,
num_range = num_range,
one_of = one_of,
everything = everything,
last_col = last_col
)
9 changes: 9 additions & 0 deletions R/vars-pull.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,12 @@ vars_pull <- function(vars, var = -1) {

vars[[pos]]
}

# FIXME: Workaround rlang bug
is_integerish <- function(x, n = NULL) {
if (typeof(x) == "integer") return(TRUE)
if (typeof(x) != "double") return(FALSE)
if (!is.finite(x)) return(FALSE)
if (!is_null(n) && length(x) != n) return(FALSE)
all(x == as.integer(x))
}
36 changes: 36 additions & 0 deletions man/vars_select_helpers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d3e2ad7

Please sign in to comment.