https://github.com/cran/bayestestR
Raw File
Tip revision: 40f7c88ddf855896018cf20ec8a7ac5fbd0ea2fb authored by Dominique Makowski on 27 January 2020, 05:30 UTC
version 0.5.1
Tip revision: 40f7c88
utils_flatten_list.R
#' Flatten a list
#'
#' @param object A list.
#' @param name Name of column of keys in the case the output is a dataframe.
#' @keywords internal
.flatten_list <- function(object, name = "name") {
  if (length(object) == 1) {
    object[[1]]
  } else if (all(sapply(object, is.data.frame))) {
    if (is.null(names(object))) {
      as.data.frame(t(sapply(object, rbind)))
    } else {
      tryCatch(
        {
          rn <- names(object)
          object <- do.call(rbind, object)
          object[name] <- rn
          object[c(name, setdiff(names(object), name))]
        },
        warning = function(w) {
          object
        },
        error = function(e) {
          object
        }
      )
    }
  } else {
    object
  }
}
back to top