Skip to content

Commit 2a7e209

Browse files
committed
Added a function enlist() to wrap objects that are not already lists into a list.
1 parent 618f3fa commit 2a7e209

File tree

4 files changed

+135
-2
lines changed

4 files changed

+135
-2
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: statnet.common
2-
Version: 4.13.0-502
3-
Date: 2025-08-29
2+
Version: 4.13.0-503
3+
Date: 2025-10-09
44
Title: Common R Scripts and Utilities Used by the Statnet Project Software
55
Authors@R: c(
66
person(c("Pavel", "N."), "Krivitsky", role=c("aut","cre"), email="pavel@statnet.org", comment=c(ORCID="0000-0002-9101-3362", affiliation="University of New South Wales")),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export(decompress_rows)
8787
export(default_options)
8888
export(despace)
8989
export(empty_env)
90+
export(enlist)
9091
export(eval_lhs.formula)
9192
export(filter_rhs.formula)
9293
export(fixed.pval)

R/misc.utilities.R

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,3 +1265,71 @@ replace <- function(x, list, values, ...) {
12651265
#' @rdname replace
12661266
#' @export
12671267
`replace<-` <- function(x, list, ..., value) replace(x, list, value, ...)
1268+
1269+
#' Wrap an object into a singleton list if not already a list
1270+
#'
1271+
#' This function tests whether its first argument is a list according
1272+
#' to the specified criterion; if not, puts it into a list of length 1.
1273+
#'
1274+
#' @param x an object to be wrapped.
1275+
#'
1276+
#' @param test how a string or a function to decide whether an object
1277+
#' counts as a list; see Details.
1278+
#'
1279+
#' @details `test` can be one of the following \describe{
1280+
#'
1281+
#' \item{`"inherits"`}{use [`inherits`]`(x, "list")`. This will
1282+
#' require the object to have class `list` and is generally the
1283+
#' strictest (i.e., will wrap the most objects).}
1284+
#'
1285+
#' \item{`"list"`}{use [`is.list`]`(x)`. This will treat S3 objects
1286+
#' based on lists as lists.}
1287+
#'
1288+
#' \item{`"vector"`}{use [`is.vector`]`(x)`. This will treat atomic
1289+
#' vectors and [`expression`]s as lists.}
1290+
#'
1291+
#' \item{a function with 1 argument}{call `as.logical(test(x))`; if
1292+
#' `TRUE`, the object is treated as a list; otherwise not.}
1293+
#'
1294+
#' }
1295+
#'
1296+
#' @examples
1297+
#'
1298+
#' data(mtcars)
1299+
#' stopifnot(
1300+
#' # Atomic vectors don't inherit from lists.
1301+
#' identical(enlist(1:3), list(1:3)),
1302+
#' # Atomic vectors are not lists internally.
1303+
#' identical(enlist(1:3, "list"), list(1:3)),
1304+
#' # Atomic vectors are a type of R vector.
1305+
#' identical(enlist(1:3, "vector"), 1:3),
1306+
#' # Data frames don't inherit from lists.
1307+
#' identical(enlist(mtcars), list(mtcars)),
1308+
#' # Data frames are lists internally.
1309+
#' identical(enlist(mtcars, "list"), mtcars),
1310+
#' # Data frames are not considered R vectors.
1311+
#' identical(enlist(mtcars, "vector"), list(mtcars))
1312+
#' )
1313+
#'
1314+
#' # We treat something as a "list" if its first element is odd.
1315+
#' is.odd <- function(x) as.logical(x[1] %% 2)
1316+
#' stopifnot(
1317+
#' # 1 is a list.
1318+
#' identical(enlist(1, is.odd), 1),
1319+
#' # 2 is not.
1320+
#' identical(enlist(2, is.odd), list(2))
1321+
#' )
1322+
#'
1323+
#' @export
1324+
enlist <- function(x, test = c("inherits", "vector", "list")) {
1325+
if (is.character(test))
1326+
test <- switch(match.arg(test),
1327+
vector = is.vector,
1328+
list = is.list,
1329+
inherits = function(x) inherits(x, "list"))
1330+
else if (!as.logical(is.function(test)))
1331+
stop(sQuote("test"), " is neither a function nor one of ",
1332+
paste.and(dQuote(formals(enlist)$test), "or"))
1333+
1334+
if (test(x)) x else list(x)
1335+
}

man/enlist.Rd

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)