R/flatten2.R

Defines functions flatten2

Documented in flatten2

#' Flatten a list without type coercion
#'
#' @section Changed Feb 19, 2015 by njf:
#' Rather than calculating length, preallocate more than needed. 
#'
#' @param x a nested list
#' @param len guess of output length
#'
#' @author \href{http://stackoverflow.com/users/662787/tommy}{Tommy}, 
#'   \href{http://stackoverflow.com/users/271616/joshua-ulrich}{Joshua Ulrich}, 
#'   \href{http://stackoverflow.com/users/980833/josh-obrien}{Josh O'Brien}, 
#'   Neal Fultz
#' @references \url{http://stackoverflow.com/questions/8139677/how-to-flatten-a-list-to-a-list-without-coercion}
#' @export

flatten2 <- function(x, len=1024) {
  y <- vector('list', len)
  i <- 1L
  rapply(x, function(x) { y[[i]] <<- x; i <<- i + 1L })
  y[seq_len(i - 1L)]
}

Try the stackoverflow package in your browser

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

stackoverflow documentation built on Jan. 10, 2020, 9:07 a.m.