The update contains some breaking changes and brings a lot of new features and operators, which markedly improves using containers in both interactive R sessions and code development. Also there is a new class dict.table to support the data.table package.
container
?A container can be considered as an enhanced version of base R’s list with a carefully designed set of extract, replace, and remove operations. They enable an easier and safer way to work with list-like data structures in interactive R sessions and in particular when developing critical code.
This blog post can only provide a glimpse of all the things you can do with containers so here the focus is on some features suitable for interactive R sessions.
Let’s start by comparing the print output of container and list (click on the panels below).
print(co)
[colors = ("Red" "Green" "Blue"), numbers = (1L 2L 3L 4L ...),
data = <<data.frame(10x2)>>]
As you can see the container by default prints very compact. Next, take a look at data extraction.
A container provides all familiar extract operations known from base R lists but also some new operations to make life (hopefully) easier. In contrast to base R lists, undefined indices are ignored and thus do not produce NULL
values.
co[[1]] # standard
[1] "Red" "Green" "Blue"
co[2:3] # standard
[numbers = (1L 2L 3L 4L ...), data = <<data.frame(10x2)>>]
co[c(1:2, 5:8)] # ignore undefined indices
[colors = ("Red" "Green" "Blue"), numbers = (1L 2L 3L 4L ...)]
co[1, 2, 5:8] # pass any number of indices - same as before
[colors = ("Red" "Green" "Blue"), numbers = (1L 2L 3L 4L ...)]
co[1, "data", 2, "foo"] # index types can be mixed
[colors = ("Red" "Green" "Blue"), data = <<data.frame(10x2)>>,
numbers = (1L 2L 3L 4L ...)]
li[[1]] # standard
[1] "Red" "Green" "Blue"
li[2:3] # standard
$numbers
[1] 1 2 3 4 5 6 7 8 9 10
$data
speed dist
1 4 2
2 4 10
3 7 4
4 7 22
5 8 16
6 9 10
7 10 18
8 10 26
9 10 34
10 11 17
li[c(1:2, 5:8)] # pad NULLs for undefined indices
$colors
[1] "Red" "Green" "Blue"
$numbers
[1] 1 2 3 4 5 6 7 8 9 10
$<NA>
NULL
$<NA>
NULL
$<NA>
NULL
$<NA>
NULL
li[1, 2, 5:8] # not supported
Error in li[1, 2, 5:8]: incorrect number of dimensions
li[1, "data", 2, "foo"] # not supported
Error in li[1, "data", 2, "foo"]: incorrect number of dimensions
In the same way, a container provides both familiar and new operations for interactive element replacement.
co[2:3] <- NA # standard
co
[colors = ("Red" "Green" "Blue"), numbers = NA, data = NA]
co[[1]] <- "" # standard
co
[colors = "", numbers = NA, data = NA]
co[["colors"]] <- "red" # standard
co
[colors = "red", numbers = NA, data = NA]
[colors = "blue", numbers = (1L 2L 3L 4L), data = NA]
co[[{"blue"}]] <- "green" # replace by value
co
[colors = "green", numbers = (1L 2L 3L 4L), data = NA]
co[[{NA}]] <- 0 # replace by value
co
[colors = "green", numbers = (1L 2L 3L 4L), data = 0]
li[2:3] <- NA # standard
li
$colors
[1] "Red" "Green" "Blue"
$numbers
[1] NA
$data
[1] NA
li[[1]] <- "" # standard
li
$colors
[1] ""
$numbers
[1] NA
$data
[1] NA
li[["colors"]] <- "red" # standard
co
[colors = "green", numbers = (1L 2L 3L 4L), data = 0]
Error in li[list("colors", 2)] <- list("blue", 1:4): invalid subscript type 'list'
#li[[{"blue"}]] <- "green" # not supported
#li[[{NA}]] <- 0 # not supported
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 ...".