#' Coerce Environment to List (Recursively). #' #' Recursively coerces an \code{environment} to a \code{list}. #' #' @param src A an \code{environment} that should be coerced. #' @param order.type A \code{character} vector (length: max=1) specifying if #' the list elements should be ordered and if so, what type of ordering should #' be applied. #' @param ... Further args. #' @return A named \code{list} that corresponds to the recursively coerced #' initial \code{environment}. #' @callGraphPrimitives #' @author Janko Thyson \email{janko.thyson.rstuff@@googlemail.com} #' @seealso \code{\link{flatten}} #' @example inst/examples/envirAsList.R envirToList <- function( src, order.type=c("increasing", "decreasing", "none"), ... ){ if(length(order.type) > 1){ order.type <- order.type[1] } if(class(src) == "environment"){ envir <- new.env() src <- as.list(src) # LOOP OVER ELEMENTS out <- lapply(seq(along=src), function(x.src){ envir$names <- c(envir$names, names(src[x.src])) # RECURSIVE FLATTENING out <- envirToList(src[[x.src]]) return(out) }) names(out) <- envir$names if(order.type == "increasing"){ idx.order <- order(names(out)) out <- out[idx.order] } if(order.type == "decreasing"){ idx.order <- order(names(out), decreasing=TRUE) out <- out[idx.order] } # / } else { out <- src } return(out) }