In an interactive R session a container can be used similar to a base
R list
, but also provides some extra features. For easier
typing it’s recommended to use the shorter cont
.
The container print method is designed to be very compact.
For more verbose output, either convert to a base list
…
or use str
.
Both length
and names
work as usual.
A container can also be constructed from a list.
Elements can be added by concatenation,
c(co, c = 3, d = 4)
# [A = 1, b = (1L 2L 3L 4L ...), c = 3, d = 4]
c(co, co2)
# [A = 1, b = (1L 2L 3L 4L ...), x = (1 2), y = (1 4)]
or name,
and containers can be nested.
In contrast to base R list
, elements cannot be added via
positional index if it exceeds the container’s length.
Single or multiple value replacement works as usual.
In contrast to base list
, containers can take a mix of
numeric and character indices.
co[list("A", 2, "c")] <- list(1, 2, "three")
co
# [A = 1, b = 2, c = "three", co2 = [x = (1 2), y = (1 4)]]
Another option is to replace by value.
This works for any data type.
The following standard access operators can be applied.
At this point, neither the $
operator nor negative
indices1
are supported. They just give empty results.
For now, a workaround for negative indexing is to temporarily convert to a list.
As another option, you can pass any number of indices, possibly mixed as numeric and character.
Invalid indices don’t produce NULL
s but are just
ignored.
This vignette showcases how {container} enhances interactive R workflows by combining the familiarity of base R list operations with additional features:
Next, see vignette Use container for code development.
Negative indexing support is planned for the next version.↩︎