R/unlist-nlist.R

Defines functions unlist.nlists unlist.nlist unlist_nlist

Documented in unlist_nlist unlist.nlist

#' Flatten nlist Object
#'
#' Simplifies an nlist object to an named numeric vector
#' where the names are the terms.
#'
#' @param x An nlist object.
#' @return A named numeric vector of the values in x.
#' @seealso [as_nlist.numeric()] and [relist_nlist()]
#' @export
#' @examples
#' unlist_nlist(nlist(y = 2, x = matrix(4:7, ncol = 2)))
unlist_nlist <- function(x) {
  chk_s3_class(x, "nlist")
  y <- unlist(unclass(x))
  if (is.null(y)) y <- numeric(0)
  y <- as.numeric(y)
  names(y) <- as_term(x)
  y
}

#' Flatten nlist Object
#'
#' @param x An nlist object.
#' @param recursive Ignored.
#' @param use.names A flag specifying whether to preserve names.
#' @return A named numeric vector of the values in x.
#' @seealso [unlist_nlist()]
#' @method unlist nlist
#' @export
#' @examples
#' unlist(nlist(y = 2, x = matrix(4:7, ncol = 2)))
unlist.nlist <- function(x, recursive = TRUE, use.names = TRUE) {
  chk_flag(use.names)
  x <- unlist_nlist(x)
  if (!use.names) x <- unname(x)
  x
}

#' @method unlist nlists
#' @export
unlist.nlists <- function(x, recursive = TRUE, use.names = TRUE) {
  stop("`unlist()` is not defined for nlists objects.")
}

Try the nlist package in your browser

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

nlist documentation built on Sept. 5, 2021, 6:05 p.m.