List environments are environments that have list-like subsetting properties such as indexed subsetting. For example,
> x <- listenv(a=2, b=3, c="hello")
> x$a
[1] 2
> x$d <- x$a + x[["b"]]
> x[[3]] <- toupper(x[[3]])
> length(x)
[1] 4
> seq_along(x)
[1] 1 2 3 4
> names(x)
[1] "a" "b" "c" "d"
> x$c
[1] "HELLO"
> x[[4]]
[1] 5
Coercing a list environment to a list:
> x <- listenv(a=2, b=3, c="hello")
`listenv` with 3 elements: 'a', 'b', 'c'
> y <- as.list(x)
> str(y)
List of 4
$ a: num 2
$ b: num 3
$ c: chr "hello"
Coercing a list to a list environment:
> z <- as.listenv(y)
`listenv` with 3 elements: 'a', 'b', 'c'
> all.equal(z, x)
[1] TRUE
Coercing a list environment to a vector (“unlisting”):
> unlist(x)
a b c
"2" "3" "hello"
> unlist(x[-3])
a b
2 3
> unlist(x[1:2], use.names=FALSE)
[1] 2 3
> x <- listenv(a=1, b=c("hello", "world"), c=list(d=42L, e=pi))
> x
`listenv` with 3 elements: 'a', 'b', 'c'
> names(x)
> str(as.list(x))
List of 3
$ a: num 1
$ b: chr [1:2] "hello" "world"
$ c:List of 2
..$ d: int 42
..$ e: num 3.14
> x <- listenv()
> length(x) <- 3
> length(x)
[1] 3
> names(x)
NULL
> str(as.list(x))
List of 3
$ : NULL
$ : NULL
$ : NULL
> x <- listenv()
> length(x) <- 3
> names(x) <- letters[1:3]
> names(x)
[1] "a" "b" "c"
> names(x)[2] <- "B"
> str(as.list(x))
List of 3
$ a: NULL
$ B: NULL
$ c: NULL
> x <- listenv()
> length(x) <- 3
> seq_along(x)
[1] 1 2 3
Here is a longer set of examples illustrating what the list environments provides:
> x <- listenv()
> x[[1]] <- { 1 }
> x[[3]] <- { "Hello world!" }
> length(x)
3
> seq_along(x)
[1] 1 2 3
> names(x) <- c("a", "b", "c")
> x[['b']]
NULL
> x$b <- TRUE
> x[[1]]
1
> str(as.list(x))
List of 3
$ a: num 1
$ b: logi TRUE
$ c: chr "Hello world!"
> x[c('a', 'c')] <- list(2, "Hello again!")
> y <- x[3:2]
> str(as.list(y))
List of 2
$ c: chr "Hello again!"
$ b: logi TRUE
Copyright Henrik Bengtsson, 2015