Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use flatten_if() with custom predicate to process input
- Loading branch information
Showing
5 changed files
with
129 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,8 +105,9 @@ NULL | |
|
||
#' @export | ||
#' @rdname bind | ||
#' @useDynLib dplyr bind_spliceable | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
lionel-
Author
Member
|
||
bind_rows <- function(..., .id = NULL) { | ||
x <- discard(list_or_dots(...), is_null) | ||
x <- flatten_if(dots_values(...), bind_spliceable) | ||
|
||
if (!length(x)) { | ||
# Handle corner cases gracefully, but always return a tibble | ||
|
@@ -117,19 +118,14 @@ bind_rows <- function(..., .id = NULL) { | |
} | ||
} | ||
|
||
for (elt in x) { | ||
if (!is_valid_df(elt) && !is_rowwise_atomic(elt) && !is_null(elt)) { | ||
abort("`...` must only contain data frames and named atomic vectors") | ||
} | ||
} | ||
|
||
if (!is_null(.id)) { | ||
if (!(is_string(.id))) { | ||
bad_args(".id", "must be a scalar string, ", | ||
"not {type_of(.id)} of length {length(.id)}" | ||
) | ||
} | ||
if (!is_named(x)) { | ||
if (!all(have_name(x) | map_lgl(x, is_empty))) { | ||
x <- compact(x) | ||
names(x) <- seq_along(x) | ||
} | ||
} | ||
|
@@ -144,22 +140,25 @@ is_df_list <- function(x) { | |
is_list(x) && every(x, inherits, "data.frame") | ||
} | ||
|
||
#' @export | ||
rbind.tbl_df <- function(..., deparse.level = 1) { | ||
bind_rows(...) | ||
} | ||
|
||
#' @export | ||
#' @rdname bind | ||
bind_cols <- function(...) { | ||
x <- discard(list_or_dots(...), is_null) | ||
x <- flatten_if(dots_values(...), bind_spliceable) | ||
out <- cbind_all(x) | ||
tibble::repair_names(out) | ||
} | ||
|
||
|
||
# Can't forward dots directly because rbind() and cbind() evaluate | ||
# them eagerly which prevents them from being captured | ||
|
||
#' @export | ||
rbind.tbl_df <- function(..., deparse.level = 1) { | ||
bind_rows(!!! list(...)) | ||
} | ||
#' @export | ||
cbind.tbl_df <- function(..., deparse.level = 1) { | ||
bind_cols(...) | ||
bind_cols(!!! list(...)) | ||
} | ||
|
||
#' @export | ||
|
@@ -173,60 +172,6 @@ combine <- function(...) { | |
} | ||
} | ||
|
||
list_or_dots <- function(...) { | ||
dots <- dots_list(...) | ||
if (!length(dots)) { | ||
return(dots) | ||
} | ||
|
||
# Old versions specified that first argument could be a list of | ||
# dataframeable objects | ||
if (is_list(dots[[1]])) { | ||
dots[[1]] <- map_if(dots[[1]], is_dataframe_like, as_tibble) | ||
} | ||
|
||
# Need to ensure that each component is a data frame or a vector | ||
# wrapped in a list: | ||
dots <- map_if(dots, is_dataframe_like, function(x) list(as_tibble(x))) | ||
dots <- map_if(dots, is_atomic, list) | ||
dots <- map_if(dots, is.data.frame, list) | ||
|
||
unlist(dots, recursive = FALSE) | ||
} | ||
|
||
is_dataframe_like <- function(x) { | ||
if (is_null(x)) | ||
return(FALSE) | ||
|
||
# data frames are not data lists | ||
if (is.data.frame(x)) | ||
return(FALSE) | ||
|
||
# Must be a list | ||
if (!is_list(x)) | ||
return(FALSE) | ||
|
||
# 0 length named list (#1515) | ||
if (!is_null(names(x)) && length(x) == 0) | ||
return(TRUE) | ||
|
||
# With names | ||
if (!is_named(x)) | ||
return(FALSE) | ||
|
||
# Where each element is an 1d vector or list | ||
if (!every(x, is_1d)) | ||
return(FALSE) | ||
|
||
# All of which have the same length | ||
n <- map_int(x, length) | ||
if (any(n != n[1])) | ||
return(FALSE) | ||
|
||
TRUE | ||
} | ||
|
||
|
||
# Deprecated functions ---------------------------------------------------- | ||
|
||
#' @export | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@lionel-: Could you please explain the function call mechanism here, and how this interacts with native function registration (#2146) ? Why didn't you use the regular
[[Rcpp::export]]
?