Skip to content

Commit

Permalink
feat: Add cleaner interface to evaluating select_positions
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaneastwood committed Oct 22, 2020
1 parent 8ae00eb commit 29d08e1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: poorman
Type: Package
Title: A Poor Man's Dependency Free Recreation of 'dplyr'
Version: 0.2.2.2
Version: 0.2.2.3
Authors@R: person("Nathan", "Eastwood", "", "[email protected]",
role = c("aut", "cre"))
Maintainer: Nathan Eastwood <[email protected]>
Expand Down
4 changes: 2 additions & 2 deletions R/relocate.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ relocate <- function(.data, ..., .before = NULL, .after = NULL) {
col_pos <- select_positions(.data, ...)

.before <- deparse_var(.before)
if (!is.null(.before)) .before <- colnames(.data)[do.call(select_positions, list(.data, substitute(.before)))]
if (!is.null(.before)) .before <- colnames(.data)[eval_select_pos(.data, .before)]
.after <- deparse_var(.after)
if (!is.null(.after)) .after <- colnames(.data)[do.call(select_positions, list(.data, substitute(.after)))]
if (!is.null(.after)) .after <- colnames(.data)[eval_select_pos(.data, .after)]

has_before <- !is.null(.before)
has_after <- !is.null(.after)
Expand Down
2 changes: 1 addition & 1 deletion R/rename.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ rename_with <- function(.data, .fn, .cols = everything(), ...) {
if (!is.function(.fn)) stop("`", .fn, "` is not a valid function")
grouped <- inherits(.data, "grouped_data")
if (grouped) grp_pos <- which(colnames(.data) %in% group_vars(.data))
col_pos <- do.call(select_positions, list(.data = .data, substitute(.cols)))
col_pos <- eval_select_pos(.data = .data, .group_pos = TRUE, .cols = substitute(.cols))
cols <- colnames(.data)[col_pos]
new_cols <- .fn(cols, ...)
if (any(duplicated(new_cols))) {
Expand Down
10 changes: 10 additions & 0 deletions R/select_positions.R
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ select_context <- function(expr) {
eval(expr, envir = select_env$.data)
}

# -- Environment ---------------------------------------------------------------

select_env <- new.env()
select_env$setup <- function(.data, calling_frame) {
select_env$.data <- .data
Expand All @@ -160,3 +162,11 @@ select_env$clean <- function() {
select_env$get_colnames <- function() colnames(select_env$.data)
select_env$get_nrow <- function() nrow(select_env$.data)
select_env$get_ncol <- function() ncol(select_env$.data)

# -- Helpers -------------------------------------------------------------------

#' A cleaner interface to evaluating select_positions when column names are not passed via ...
#' @noRd
eval_select_pos <- function(.data, .cols, .group_pos = FALSE) {
do.call(select_positions, list(.data = .data, .group_pos = .group_pos, .cols))
}

0 comments on commit 29d08e1

Please sign in to comment.