Skip to content

Commit

Permalink
New function f_pull.
Browse files Browse the repository at this point in the history
  • Loading branch information
NicChr committed Nov 28, 2024
1 parent df04028 commit 792caf4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export(f_group_by)
export(f_inner_join)
export(f_left_join)
export(f_nest_by)
export(f_pull)
export(f_rename)
export(f_right_join)
export(f_rowwise)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ in the list over environment objects.
evaluates its argument dynamically unless the data is grouped and the
expressions supplied aren't simply column selections.

- New function `f_pull` as a fast convenience function for extracting
vectors from columns.

- `f_arrange` gains the `.descending` argument to efficiently
return data frames in descending order.

Expand Down
7 changes: 5 additions & 2 deletions R/abc.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ which_in <- get_from_package("which_in", "cheapr")
cpp_int64_to_numeric <- get_from_package("cpp_int64_to_numeric", "cheapr")
cpp_loc_set_replace <- get_from_package("cpp_loc_set_replace", "cheapr")
named_list <- get_from_package("named_list", "cheapr")
`%in_%` <- cheapr::`%in_%`

check_length <- function(x, size){
if (length(x) != size){
Expand Down Expand Up @@ -60,8 +61,10 @@ col_select_pos <- function(data, .cols = character()){
if (is.null(out_nms)){
names(out) <- .subset(data_nms, out)
} else {
es <- !nzchar(out_nms)
out_nms[es] <- .subset(data_nms, .subset(out, es))
es <- empty_str_locs(out_nms)
if (length(es)){
out_nms[es] <- .subset(data_nms, .subset(out, es))
}
names(out) <- out_nms
}
out
Expand Down
11 changes: 10 additions & 1 deletion R/f_select.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Fast `dplyr::select()`/`dplyr::rename()`
#' Fast 'dplyr' `select()`/`rename()`/`pull()`
#'
#' @description
#' `f_select()` operates the exact same way as `dplyr::select()` and
Expand Down Expand Up @@ -116,4 +116,13 @@ f_rename.data.table <- function(data, ..., .cols = NULL){
}
#' @rdname f_select
#' @export
f_pull <- function(data, ..., .cols = NULL){
col <- tidy_select_pos(data, ..., .cols = .cols)
if (length(col) != 1){
stop("You must select exactly one column in `f_pull()`")
}
.subset2(data, col)
}
#' @rdname f_select
#' @export
nothing <- function() character()
11 changes: 4 additions & 7 deletions R/tidy_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,10 @@ tidy_select_pos <- function(data, ..., .cols = NULL){
if (!is.null(.cols)){
out <- col_select_pos(data, .cols)
} else {
dots <- rlang::enquos(...)
labels <- quo_labels(dots)
if (all(labels %in% names(data))){
out <- add_names(match(labels, names(data)), names(labels))
# if (is.null(names(out))){
# names(out) <- labels
# }
dots <- rlang::quos(...)
labels <- quo_labels(dots, named = TRUE)
if (all(labels %in_% data_nms)){
out <- add_names(match(labels, data_nms), names(labels))
is_dup <- collapse::fduplicated(list(names(out), unname(out)))
out <- out[!is_dup]
if (anyDuplicated(names(out))){
Expand Down
5 changes: 4 additions & 1 deletion man/f_select.Rd

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

0 comments on commit 792caf4

Please sign in to comment.