Skip to content

Commit

Permalink
Updating documentation, examples and tests for Fix 1096. No longer re…
Browse files Browse the repository at this point in the history
…quires R4.1 for implementation.
  • Loading branch information
SokolovAnatoliy committed Aug 15, 2024
1 parent 79cc196 commit 53881b3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
8 changes: 5 additions & 3 deletions R/list-combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +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 to keep empty elements of a list as NA.
#' @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
Expand All @@ -32,13 +33,14 @@
#' x2 <- list(
#' a = data.frame(x = 1:2),
#' b = data.frame(y = "a"),
#' c = data.frame(z = NULL)
#' 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_cbind(x2, keep_empty = TRUE)
#'
list_c <- function(x, ..., ptype = NULL, keep_empty = FALSE) {
vec_check_list(x)
Expand Down Expand Up @@ -106,5 +108,5 @@ check_list_of_data_frames <- function(x, error_call = caller_env()) {

## used to convert empty elements into NA for list_binding functions
convert_empty_element_to_NA = function(x) {
map(x, \(x) if(vctrs::vec_is_empty(x)) NA else x)
map(x, function(x) if(vctrs::vec_is_empty(x)) NA else x)
}
4 changes: 2 additions & 2 deletions tests/testthat/test-list-combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ test_that("NULLs are converted to NA when keep_empty = TRUE", {
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), vec_rbind(df1, NA, df1))
expect_equal(list_cbind(list(df1, z = NULL, df2), keep_empty = TRUE), vec_cbind(df1, z = NA, df2))
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))
})

test_that("empty inputs return expected output", {
Expand Down

0 comments on commit 53881b3

Please sign in to comment.