#' @title Tag a list of data.frames.
#'
#' @description Takes a list of data.frames and "tag" each my adding a column
#' containing the name of the list container they belong to. The tags can later
#' be used as a factor when "fusing" the list in a single data.frame.
#'
#' @param list_df A list of data.frames.
#' @param label A single character string containing the name of the tagged column.
#'
#' @return A tagged list of data.frames.
#'
#' @importFrom purrr map_dbl map2 pmap
#' @export
tag <- function(list_df, label = "tag"){
it <- purrr::map_dbl(list_df, nrow)
l_tag <- purrr::map2(names(list_df), it, ~ rep(.x, each = .y))
l_args <- list(list_df, label, l_tag)
tag_atom <- function(x, y, z){
x[[y]] <- z
return(x)
}
list_df <- purrr::pmap(l_args, tag_atom)
return(list_df)
}
#' @title Fuse list of data.frames
#'
#' @description Applies purrr::reduce(rbind) to a list of data.frames with the
#' same columns.
#'
#' @param list_df A list of data.frames with the same columns.
#'
#' @return A fused data.frame
#'
#' @importFrom purrr reduce
#' @export
fuse <- function(list_df){
purrr::reduce(list_df, rbind)
}
#' Tag and fuse workflow
#'
#' @param list_df A list of data.frames with the same columns.
#'
#'
#' @export
tagnfuse <- function(list_df, label = "tag"){
fuse(tag(list_df, label = label))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.