-
Notifications
You must be signed in to change notification settings - Fork 275
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The key idea is to introduce a new family of "combining" functions: `list_c()`, `list_rbind()`, and `list_cbind()`, which replace `flatten_lgl()`, `flatten_int()`, `flatten_dbl()`, `flatten_chr()` (now `list_c()`), `flatten_dfc()` (`list_cbind()`), and `flatten_dfr()` (`list_rbind()`). The new functions are straightforward wrappers around vctrs functions, but somehow feel natural in purrr to me. This leaves `flatten()`, which had a rather idiosyncratic interface. It's now been replaced by `list_flatten()` which now always removes a single layer of list hierarchy (and nothing else). While working on this I realised that this was actually what `splice()` did, so overall this feels like a major improvement in naming consistency. With those functions in place we can deprecate `map_dfr()` and `map_dfc()` which are actually "flat" map functions because they combine, rather than simplify, the results. They have never actually belonged with `map_int()` and friends because they don't satisfy the invariant `length(map(.x, .f)) == length(.x)`, and `.f` must return a length-1 result. This also strongly implies that `flat_map()` would just be `map_c()` and is thus not necessary. * Fixes #376 by deprecating `map_dfc()` * Fixes #405 by clearly ruling against `map_c()` * Fixes #472 by deprecating `map_dfr()` * Fixes #575 by introducing `list_c()`, `list_rbind()`, and `list_cbind()` * Fixes #757 by deprecating `flatten_dfr()` and `flatten_dfc()` * Fixes #758 by introducing `list_rbind()` and `list_cbind()` * Part of #900
- Loading branch information
Showing
46 changed files
with
1,291 additions
and
569 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
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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#' Combine list elements into a single data structure | ||
#' | ||
#' @description | ||
#' * `list_c()` combines elements into a vector by concatenating them together | ||
#' with [vctrs::vec_c()]. | ||
#' | ||
#' * `list_rbind()` combines elements into a data frame by row-binding them | ||
#' together with [vctrs::vec_rbind()]. | ||
#' | ||
#' * `list_cbind()` combines elements into a data frame by column-binding them | ||
#' together with [vctrs::vec_cbind()]. | ||
#' | ||
#' @param x A list. For `list_rbind()` and `list_cbind()` the list must | ||
#' only contain data frames. | ||
#' @param ptype An optional prototype to ensure that the output type is always | ||
#' the same. | ||
#' @param id By default, `names(x)` are lost. Alternatively, supply a string | ||
#' and the names will be saved into a column with name `{id}`. If `id` | ||
#' is supplied and `x` is not named, the position of the elements will | ||
#' be used instead of the names. | ||
#' @param size An optional integer size to ensure that every input has the | ||
#' 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. | ||
#' @export | ||
#' @examples | ||
#' x1 <- list(a = 1, b = 2, c = 3) | ||
#' list_c(x1) | ||
#' | ||
#' x2 <- list( | ||
#' a = data.frame(x = 1:2), | ||
#' b = data.frame(y = "a") | ||
#' ) | ||
#' list_rbind(x2) | ||
#' list_rbind(x2, id = "id") | ||
#' list_rbind(unname(x2), id = "id") | ||
#' | ||
#' list_cbind(x2) | ||
list_c <- function(x, ptype = NULL) { | ||
vec_check_list(x) | ||
vctrs::vec_unchop(x, ptype = ptype) | ||
} | ||
|
||
#' @export | ||
#' @rdname list_c | ||
list_cbind <- function( | ||
x, | ||
name_repair = c("unique", "universal", "check_unique"), | ||
size = NULL | ||
) { | ||
check_list_of_data_frames(x) | ||
|
||
vctrs::vec_cbind(!!!x, .name_repair = name_repair, .size = size, .call = current_env()) | ||
} | ||
|
||
#' @export | ||
#' @rdname list_c | ||
list_rbind <- function(x, id = rlang::zap(), ptype = NULL) { | ||
check_list_of_data_frames(x) | ||
|
||
vctrs::vec_rbind(!!!x, .names_to = id, .ptype = ptype, .call = current_env()) | ||
} | ||
|
||
|
||
check_list_of_data_frames <- function(x, error_call = caller_env()) { | ||
vec_check_list(x, call = error_call) | ||
|
||
is_df <- map_lgl(x, is.data.frame) | ||
|
||
if (all(is_df)) { | ||
return() | ||
} | ||
|
||
bad <- which(!is_df) | ||
cli::cli_abort( | ||
"All elements of {.arg x} must be data frames. Elements {bad} are not.", | ||
call = error_call | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#' Flatten a list | ||
#' | ||
#' Flattening a list removes a single layer of internal hierarchy, | ||
#' i.e. it inlines elements that are lists leaving non-lists alone. | ||
#' | ||
#' @param x A list. | ||
#' @param name_spec If both inner and outer names are present, control | ||
#' how they are combined. Should be a glue specification that uses | ||
#' variables `inner` and `outer`. | ||
#' @param name_repair One of `"minimal"`, `"unique"`, `"universal"`, or | ||
#' `"check_unique"`. See [vctrs::vec_as_names()] for the meaning of these | ||
#' options. | ||
#' @return A list. The list might be shorter if `x` contains empty lists, | ||
#' the same length if it contains lists of length 1 or no sub-lists, | ||
#' or longer if it contains lists of length > 1. | ||
#' @export | ||
#' @examples | ||
#' x <- list(1, list(2, 3), list(4, list(5))) | ||
#' x %>% list_flatten() %>% str() | ||
#' x %>% list_flatten() %>% list_flatten() %>% str() | ||
#' | ||
#' # Flat lists are left as is | ||
#' list(1, 2, 3, 4, 5) %>% list_flatten() %>% str() | ||
#' | ||
#' # Empty lists will disappear | ||
#' list(1, list(), 2, list(3)) %>% list_flatten() %>% str() | ||
#' | ||
#' # Another way to see this is that it reduces the depth of the list | ||
#' x <- list( | ||
#' list(), | ||
#' list(list()) | ||
#' ) | ||
#' x %>% pluck_depth() | ||
#' x %>% list_flatten() %>% pluck_depth() | ||
#' | ||
#' # Use name_spec to control how inner and outer names are combined | ||
#' x <- list(x = list(a = 1, b = 2), y = list(c = 1, d = 2)) | ||
#' x %>% list_flatten() %>% names() | ||
#' x %>% list_flatten(name_spec = "{outer}") %>% names() | ||
#' x %>% list_flatten(name_spec = "{inner}") %>% names() | ||
list_flatten <- function( | ||
x, | ||
name_spec = "{outer}_{inner}", | ||
name_repair = c("minimal", "unique", "check_unique", "universal") | ||
) { | ||
vec_check_list(x) | ||
|
||
x <- map_if(x, vec_is_list, identity, .else = list) | ||
vec_unchop( | ||
x, | ||
ptype = list(), | ||
name_spec = name_spec, | ||
name_repair = name_repair | ||
) | ||
} |
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
Oops, something went wrong.