-
Notifications
You must be signed in to change notification settings - Fork 283
Adding keep_empty argument to list_c, list_cbind, and list_rbind. #1144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
79cc196
53881b3
c732e0e
47ced5c
f726831
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
#' same size (i.e. number of rows). | ||
#' @param name_repair One of `"unique"`, `"universal"`, or `"check_unique"`. | ||
#' See [vctrs::vec_as_names()] for the meaning of these options. | ||
#' @param keep_empty An optional logical. If FALSE (the default), then the empty element is silently ignored; | ||
#' if TRUE, then the empty element is kept as an NA`. | ||
#' @inheritParams rlang::args_dots_empty | ||
#' @export | ||
#' @examples | ||
|
@@ -30,17 +32,22 @@ | |
#' | ||
#' x2 <- list( | ||
#' a = data.frame(x = 1:2), | ||
#' b = data.frame(y = "a") | ||
#' b = data.frame(y = "a"), | ||
#' c = NULL) | ||
#' ) | ||
#' list_rbind(x2) | ||
#' list_rbind(x2, names_to = "id") | ||
#' list_rbind(x2, names_to = "id", keep_empty = TRUE) | ||
#' list_rbind(unname(x2), names_to = "id") | ||
#' | ||
#' list_cbind(x2) | ||
list_c <- function(x, ..., ptype = NULL) { | ||
#' list_cbind(x2, keep_empty = TRUE) | ||
#' | ||
list_c <- function(x, ..., ptype = NULL, keep_empty = FALSE) { | ||
vec_check_list(x) | ||
check_dots_empty() | ||
|
||
if(keep_empty) x <- convert_empty_element_to_NA(x) | ||
|
||
# For `list_c()`, we don't expose `list_unchop()`'s `name_spec` arg, | ||
# and instead strip outer names to avoid collisions with inner names | ||
x <- unname(x) | ||
|
@@ -58,19 +65,22 @@ list_cbind <- function( | |
x, | ||
..., | ||
name_repair = c("unique", "universal", "check_unique"), | ||
size = NULL | ||
size = NULL, | ||
keep_empty = FALSE | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DavisVaughan my intuition is that |
||
) { | ||
check_list_of_data_frames(x) | ||
check_dots_empty() | ||
if(keep_empty) x <- convert_empty_element_to_NA(x) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure this applies to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To me, it's unclear what type the empty column should have. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's also weird that for |
||
|
||
vec_cbind(!!!x, .name_repair = name_repair, .size = size, .error_call = current_env()) | ||
} | ||
|
||
#' @export | ||
#' @rdname list_c | ||
list_rbind <- function(x, ..., names_to = rlang::zap(), ptype = NULL) { | ||
list_rbind <- function(x, ..., names_to = rlang::zap(), ptype = NULL, keep_empty = FALSE) { | ||
check_list_of_data_frames(x) | ||
check_dots_empty() | ||
if(keep_empty) x <- convert_empty_element_to_NA(x) | ||
|
||
vec_rbind(!!!x, .names_to = names_to, .ptype = ptype, .error_call = current_env()) | ||
} | ||
|
@@ -95,3 +105,8 @@ check_list_of_data_frames <- function(x, error_call = caller_env()) { | |
call = error_call | ||
) | ||
} | ||
|
||
## used to convert empty elements into NA for list_binding functions | ||
convert_empty_element_to_NA = function(x) { | ||
map(x, function(x) if(vctrs::vec_is_empty(x)) NA else x) | ||
hadley marked this conversation as resolved.
Show resolved
Hide resolved
hadley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,15 @@ test_that("NULLs are ignored", { | |
expect_equal(list_cbind(list(df1, NULL, df2)), vec_cbind(df1, df2)) | ||
}) | ||
|
||
test_that("NULLs are converted to NA when keep_empty = TRUE", { | ||
df1 <- data.frame(x = 1) | ||
df2 <- data.frame(y = 1) | ||
|
||
expect_equal(list_c(list(1, NULL, 2), keep_empty = TRUE), c(1, NA, 2)) | ||
expect_equal(list_rbind(list(df1, NULL, df1), keep_empty = TRUE), data.frame(x = c(1, NA, 1))) | ||
expect_equal(list_cbind(list(df1, z = NULL, df2), keep_empty = TRUE), data.frame(df1, z = NA, df2)) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we decide to implement this then I'd like the chance to come through and write a lot of tests for the 3 cases individually. The |
||
|
||
test_that("empty inputs return expected output", { | ||
expect_equal(list_c(list()), NULL) | ||
expect_equal(list_c(list(NULL)), NULL) | ||
|
Uh oh!
There was an error while loading. Please reload this page.