container: v1.0.0 on CRAN
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
What is 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.
library(container)
co <- container(colors = c("Red", "Green", "Blue"),
numbers = 1:10,
data = cars[1:10, ])
Let’s start by comparing the print output of container and list (click on the panels below).
container
print(co)
[colors = ("Red" "Green" "Blue"), numbers = (1L 2L 3L 4L ...),
data = <<data.frame(10x2)>>]
list
li = as.list(co) print(li) $colors [1] "Red" "Green" "Blue" $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
As you can see the container by default prints very compact. Next, take a look at data extraction.
Extract
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.
container
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 ...)]
list
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
Replace
In the same way, a container provides both familiar and new operations for interactive element replacement.
container
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]
co[list("colors", 2)] <- list("blue", 1:4) # mixed indices
co
[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]
list
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]
li[list("colors", 2)] <- list("blue", 1:4) # not supported
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/.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.