#' Merges (fuses) lists recursively
#'
#' Merges lists so that alike-named elements at the same level of list hierarchy
#' receive the values that come last in the list of arguments.
#'
#' Side effect of this implementation is that elements get sorted on every level.
#'
#' @param ... lists to be fused
#' @return merged list (or vector if arguments are vectors)
#'
#' @examples
#' fuse(1, 2) # Returns 2
#' # Example 1
#' # x = 1 gets replaced by x = list(z = NULL)
#' # y = 0 gets replaced by y = 2
#' fuse(list(y = 0), list(x = 1, y = 2), list(x = list(z = NULL)))
#' # ... and the result equals list(x = list(z = NULL), y = 2)
#' #
#' # Example 2
#' fuse(0, list(NULL), NA, NULL) # yields NA
#' # ... because the last NULL is ignored
#' # and arguments 0, list(NULL) and NA are fused as unnamed
#' # and on the same level of list hierarchy
#'
#' @export
fuse <- function(...) {
arg <- list(...)
# Get rid of NULLs
arg <- arg[!sapply(arg, is.null)]
# Last leaf wins if it's not empty
la <- length(arg)
if (la == 0) NULL else if (is_leaf(arg[[la]])) arg[[la]]
# Drop the leaves, grow named branches
else {
flat_arg <- do.call("c", arg[!sapply(arg, is_leaf, simplify = TRUE)])
lapply(split(flat_arg, get_names(flat_arg)),
function(branch) do.call(fuse, unname(branch)))
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.