The {container} package provides an enhanced version of base R’s list. Version 1.1.0 adds new extract and replace operations for interactive use to both match base R’s list behavior more closely and to introduce new convenient features beyond that.
Version 1.1.0 mainly adds new extract and replace operations.
Like base R lists, it now also supports negative complements (x[-1]) and logical
indexing (x[c(TRUE, FALSE)]). In addition, it enables range selection using
non-standard evaluation (x[a:b], x[1:c]).
For a detailed overview of all changes in this release, see the
changelog.
Negative complements and logical indexing now work as known from base R lists.
co <- container(a = 1, b = 2, c = 3, d = 4)
co[-1]
[b = 2, c = 3, d = 4]
co[-(1:3)]
[d = 4]
co[-c("a", "d")]
[b = 2, c = 3]
mask <- c(TRUE, FALSE)
co[mask]
[a = 1, c = 3]
mask2 <- c(TRUE, NA)
co[mask2] # NA treated as FALSE (warning) -> [a = 1, c = 3]
[a = 1, c = 3]
Range selection using NSE works with names and numbers, including mixed usage, reversed order, and negative complements.
co[a:b]
[a = 1, b = 2]
co[1:c]
[a = 1, b = 2, c = 3]
co[d:2]
[d = 4, c = 3, b = 2]
co[-(b:c)]
[a = 1, d = 4]
Warning: while NSE selections are perfect for interactive work where clarity beats ceremony, for serious code, explicit indices are preferred.
Replacing values works similarly to extraction.
co <- container(a = 1, b = 2, c = 3, d = 4)
co[-(b:c)] <- 0
co
[a = 0, b = 2, c = 3, d = 0]
co[c(TRUE, FALSE)] <- 9
co
[a = 9, b = 2, c = 9, d = 0]
As the container serves as the base class for all other classes in the
package, all new extraction and replacement operations are also available
for deque, set, and dict objects.
s <- setnew(a = 1, b = 2, c = 3, d = 4)
s[-(b:c)]
{a = 1, d = 4}
params <- dict(a = 1, x = 3, b = "foo", y = "bar")
params # dicts are always sorted by key
{a = 1, b = "foo", x = 3, y = "bar"}
params[a:x]
{a = 1, b = "foo", x = 3}
To see the full official documentation, visit https://rpahl.github.io/container/.
Text and figures are licensed under Creative Commons Attribution CC BY-SA 4.0. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".