R/bind_or_combine.R

Defines functions repair_col_names bind_or_combine

#' @importFrom dplyr bind_rows
#' @importFrom tibble as_tibble
#' @importFrom purrr map
bind_or_combine <- function(...) {
  .list <- list(...)

  if (is.matrix(.list[[1]])) {
    .list <- purrr::map(
      .list,
      purrr::partial(tibble::as_tibble, .name_repair = repair_col_names)
    )
  }
  if (is.null(names(.list[[1]]))) {
    return(tibble::enframe(vctrs::vec_c(!!!.list), name = NULL))
  }
  do.call("bind_rows", .list)
}

repair_col_names <- function(x) {
  names(x) <- x
  names(tibble::repair_names(x))
}

Try the simTool package in your browser

Any scripts or data that you put into this service are public.

simTool documentation built on Jan. 8, 2021, 2:25 a.m.