R/append_list.R

Defines functions append_list

Documented in append_list

#' Append R object to a list.
#'
#' @param l A R-list.
#' @param obj R object to be appended to the list.
#' @param obj.name R object name to be used to the list.
#' @param overwrite logical value indicates whether overwrite object with the same name.
#' @return
#'   \item{l_new}{A new list with additions.}
#' @seealso \code{\link{hcl}}
#' @examples
#' l <- list(c("a", "b"))
#' x <- list(c(1:5))
#' append_list(l, x, "X")
#' @export

append_list <- function(l, obj, obj.name, overwrite = TRUE){

  if (!is.list(obj)) obj <- list(obj)
  # l[[obj.name]] may not be a list, but l[obj.name] is
  if (!is.null(l[obj.name]) & overwrite==TRUE){
    l_new <- l
    l_new[obj.name] <- obj
  } else {
    l_new <- c(l, obj)
    names(l_new)[length(l_new)] <- obj.name
  }
  return(l_new)
}
kang-yu/lazyu documentation built on May 30, 2019, 11:42 a.m.