listenv/ 0000755 0001762 0000144 00000000000 12640067456 011750 5 ustar ligges users listenv/inst/ 0000755 0001762 0000144 00000000000 12640067035 012716 5 ustar ligges users listenv/inst/doc/ 0000755 0001762 0000144 00000000000 12640067035 013463 5 ustar ligges users listenv/inst/doc/listenv.html 0000644 0001762 0000144 00000066026 12640067035 016047 0 ustar ligges users
List environments are environments that have list-like properties. They are implemented by the listenv package. The main features of a list environment are summarized in the below table:
Property | list environments | lists | environments |
---|---|---|---|
Number of elements, e.g. length() |
yes | yes | yes |
Named elements, e.g. names() , x$a and x[["a"]] |
yes | yes | yes |
Duplicated names | yes | yes | |
Indexed elements, e.g. x[[4]] |
yes | yes | |
Dimensions, e.g. dim(x) |
yes | yes | |
Names of dimensions, e.g. dimnames(x) |
yes | yes | |
Indexing by dimensions, e.g. x[[2,4]] and x[[2,"D"]] |
yes | yes | |
Multi-element subsetting, e.g. x[c("a", "c")] , x[-1] , [2:1,,3] |
yes | yes | |
Multi-element subsetting preserves element names | yes | ||
Removing element by assigning NULL, e.g. x$c <- NULL |
yes | yes | |
Mutable, e.g. y <- x; y$a <- 3; identical(y, x) |
yes | yes | |
Compatible* with assign() , delayedAssign() , get() and exists() |
yes | yes |
For example,
> x <- listenv(a = 1, b = 2, c = "hello")
> x
A 'listenv' vector with 3 elements ('a', 'b', 'c').
> length(x)
[1] 3
> names(x)
[1] "a" "b" "c"
> x$a
[1] 1
> x[[3]] <- toupper(x[[3]])
> x$c
[1] "HELLO"
> y <- x
> y$d <- y$a + y[["b"]]
> names(y)[2] <- "a"
> y$a
[1] 1
> y
A 'listenv' vector with 4 elements ('a', 'a', 'c', 'd').
> identical(y, x)
[1] TRUE
> for (ii in seq_along(x)) {
+ cat(sprintf("Element %d (%s): %s\n", ii, sQuote(names(x)[ii]),
+ x[[ii]]))
+ }
Element 1 ('a'): 1
Element 2 ('a'): 2
Element 3 ('c'): HELLO
Element 4 ('d'): 3
> x[c(1, 3)] <- list(2, "Hello world!")
> x
A 'listenv' vector with 4 elements ('a', 'a', 'c', 'd').
> y <- as.list(x)
> str(y)
List of 4
$ a: num 2
$ a: num 2
$ c: chr "Hello world!"
$ d: num 3
> z <- as.listenv(y)
> z
A 'listenv' vector with 4 elements ('a', 'a', 'c', 'd').
> identical(z, x)
[1] FALSE
> all.equal(z, x)
[1] TRUE
List environments are created similarly to lists but also similarly to environments. To create an empty list environment, use
> x <- listenv()
> x
A 'listenv' vector with 0 elements.
This can later can be populated using named assignments,
> x$a <- 1
> x
A 'listenv' vector with 1 element ('a').
comparable to how both lists and environments work. Similarly to lists, they can also be populated using indices, e.g.
> x[[2]] <- 2
> x$c <- 3
> x
A 'listenv' vector with 3 elements ('a', '', 'c').
Just as for lists, a list environment is expanded with NULL
elements whenever a new element is added that is beyond the current length plus one, e.g.
> x[[5]] <- 5
> x
A 'listenv' vector with 5 elements ('a', '', 'c', '', '').
> x[[4]]
NULL
As with lists, the above list environment can also be created from the start, e.g.
> x <- listenv(a = 1, 3, c = 4, NULL, 5)
> x
A 'listenv' vector with 5 elements ('a', '', 'c', '', '').
As for lists, the length of a list environment can at any time be increased or decreased by assigning it a new length. If decreased, elements are dropped, e.g.
> x
A 'listenv' vector with 5 elements ('a', '', 'c', '', '').
> length(x) <- 2
> x
A 'listenv' vector with 2 elements ('a', '').
> x[[1]]
[1] 1
> x[[2]]
[1] 3
If increased, new elements are populated with unnamed elements of NULL
, e.g.
> length(x) <- 4
> x
A 'listenv' vector with 4 elements ('a', '', '', '').
> x[[3]]
NULL
> x[[4]]
NULL
To allocate an “empty” list environment (with all NULL
:s) of a given length, do
> x <- listenv()
> length(x) <- 4
> x
A 'listenv' vector with 4 unnamed elements.
Note: Unfortunately, it is not possible to use x <- vector("listenv", length=4)
; that construct is only supported for the basic data types.
Analogously to lists and plain environments, it is possible to iterate over elements of list environments by the element names. For example,
> x <- listenv(a = 1, b = 2, c = 3)
> for (name in names(x)) {
+ cat(sprintf("Element %s: %s\n", sQuote(name), x[[name]]))
+ }
Element 'a': 1
Element 'b': 2
Element 'c': 3
Analogously to lists, but contrary to plain environments, it is also possible to iterate over elements by their indices. For example,
> x <- listenv(a = 1, b = 2, c = 3)
> for (ii in seq_along(x)) {
+ cat(sprintf("Element %d: %s\n", ii, x[[ii]]))
+ }
Element 1: 1
Element 2: 2
Element 3: 3
Coercing a list environment to a list:
> x <- listenv(a = 2, b = 3, c = "hello")
> x
A 'listenv' vector with 3 elements ('a', 'b', 'c').
> y <- as.list(x)
> str(y)
List of 3
$ a: num 2
$ b: num 3
$ c: chr "hello"
Coercing a list to a list environment:
> z <- as.listenv(y)
> z
A 'listenv' vector with 3 elements ('a', 'b', 'c').
> identical(z, x)
[1] FALSE
> all.equal(z, x)
[1] TRUE
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
Analogously to lists, and contrary to plain environments, list environments can have dimensions with corresponding names. For example,
> x <- as.listenv(1:6)
> dim(x) <- c(2, 3)
> dimnames(x) <- list(c("a", "b"), c("A", "B", "C"))
> x
A 'listenv' matrix with 6 unnamed elements arranged in 2x3 rows ('a', 'b') and columns ('A', 'B', 'C').
An easy way to quickly get an overview is to coerce to a list, e.g.
> as.list(x)
A B C
a 1 3 5
b 2 4 6
Individual elements of a list environment can be accessed using standard subsetting syntax, e.g.
> x[["a", "B"]]
[1] 3
> x[[1, 2]]
[1] 3
> x[[1, "B"]]
[1] 3
We can assign individual elements similarly, e.g.
> x[["b", "B"]] <- -x[["b", "B"]]
> as.list(x)
A B C
a 1 3 5
b 2 -4 6
We can also assign multiple elements through dimensional subsetting, e.g.
> x[2, -1] <- 98:99
> as.list(x)
A B C
a 1 3 5
b 2 98 99
> x["a", c(1, 3)] <- list(97, "foo")
> as.list(x)
A B C
a 97 3 "foo"
b 2 98 99
> x[] <- 1:6
> as.list(x)
A B C
a 1 3 5
b 2 4 6
Concurrently with dimensional names it is possible to have names of the invidual elements just as for list environments without dimensions. For example,
> names(x) <- letters[seq_along(x)]
> x
A 'listenv' matrix with 6 elements ('a', 'b', 'c', ..., 'f') arranged in 2x3 rows ('a', 'b') and columns ('A', 'B', 'C').
> x[["a"]]
[1] 1
> x[["f"]]
[1] 6
> x[c("a", "f")]
A 'listenv' vector with 2 elements ('a', 'f').
> unlist(x)
a b c d e f
1 2 3 4 5 6
Contrary to lists, element names are preserved also with multi-dimensional subsetting, e.g.
> x[1, 2]
A 'listenv' vector with 1 element ('c').
> x[1, 2, drop = FALSE]
A 'listenv' matrix with 1 element ('c') arranged in 1x1 rows ('a') and columns ('B').
> x[1:2, 2:1]
A 'listenv' matrix with 4 elements ('c', 'd', 'a', 'b') arranged in 2x2 rows ('a', 'b') and columns ('B', 'A').
> x[2, ]
A 'listenv' vector with 3 elements ('b', 'd', 'f').
> x[2, , drop = FALSE]
A 'listenv' matrix with 3 elements ('b', 'd', 'f') arranged in 1x3 rows ('b') and columns ('A', 'B', 'C').
> x["b", -2, drop = FALSE]
A 'listenv' matrix with 2 elements ('b', 'f') arranged in 1x2 rows ('b') and columns ('A', 'C').
Note, whenever dimensions are set using dim(x) <- dims
both the dimensional names and the element names are removed, e.g.
> dim(x) <- NULL
> names(x)
NULL
This behavior is by design, cf. help("dim", package="base")
.
The current implementation does not support dimensional subsetting of more than one element. For instance, x[1,]
is not supported by this version.
List environments are as their name suggests environments. Whenever working with environments in R, it is important to understand that environments are mutable whereas all other of the basic data types in R are immutable. For example, consider the following function that assigns zero to element a
of object x
:
> setA <- function(x) {
+ x$a <- 0
+ x
+ }
If we pass a regular list to this function,
> x <- list(a = 1)
> y <- setA(x)
> x$a
[1] 1
> y$a
[1] 0
we see that x
is unaffected by the assignment. This is because lists are immutable in R. However, if we pass an environment instead,
> x <- new.env()
> x$a <- 1
> y <- setA(x)
> x$a
[1] 0
> y$a
[1] 0
we find that x
was affected by the assignment. This is because environments are mutable in R. Since list environments inherits from environments, this also goes for them, e.g.
> x <- listenv(a = 1)
> y <- setA(x)
> x$a
[1] 0
> y$a
[1] 0
What is also important to understand is that it is not just the content of an environment that is mutable but also its attributes. For example,
> x <- listenv(a = 1)
> y <- x
> attr(y, "foo") <- "Hello!"
> attr(x, "foo")
[1] "Hello!"
Copyright Henrik Bengtsson, 2015
listenv/inst/doc/listenv.md.rsp 0000644 0001762 0000144 00000021507 12640067035 016301 0 ustar ligges users <%@meta language="R-vignette" content="-------------------------------- %\VignetteIndexEntry{List Environments} %\VignetteAuthor{Henrik Bengtsson} %\VignetteKeyword{R} %\VignetteKeyword{package} %\VignetteKeyword{vignette} %\VignetteKeyword{listenv} %\VignetteEngine{R.rsp::rsp} %\VignetteTangle{FALSE} --------------------------------------------------------------------"%> <% R.utils::use("R.utils") use("listenv") options("withCapture/newline"=FALSE) %> # <%@meta name="title"%> _List environments_ are environments that have list-like properties. They are implemented by the [listenv] package. The main features of a list environment are summarized in the below table: | Property | list environments | lists | environments | |------------------------------------------------------------------------|:-----------------:|:------:|:------------:| | Number of elements, e.g. `length()` | yes | yes | yes | | Named elements, e.g. `names()`, `x$a` and `x[["a"]]` | yes | yes | yes | | Duplicated names | yes | yes | | | Indexed elements, e.g. `x[[4]]` | yes | yes | | | Dimensions, e.g. `dim(x)` | yes | yes | | | Names of dimensions, e.g. `dimnames(x)` | yes | yes | | | Indexing by dimensions, e.g. `x[[2,4]]` and `x[[2,"D"]]` | yes | yes | | | Multi-element subsetting, e.g. `x[c("a", "c")]`, `x[-1]`, `[2:1,,3]` | yes | yes | | | Multi-element subsetting preserves element names | yes | | | | Removing element by assigning NULL, e.g. `x$c <- NULL` | yes | yes | | | Mutable, e.g. `y <- x; y$a <- 3; identical(y, x)` | yes | | yes | | Compatible* with `assign()`, `delayedAssign()`, `get()` and `exists()` | yes | | yes | For example, ```r <%=withCapture({ x <- listenv(a=1, b=2, c="hello") x length(x) names(x) x$a x[[3]] <- toupper(x[[3]]) x$c y <- x y$d <- y$a + y[["b"]] names(y)[2] <- "a" y$a y identical(y, x) for (ii in seq_along(x)) { cat(sprintf("Element %d (%s): %s\n", ii, sQuote(names(x)[ii]), x[[ii]])) } <%--- get(map(x)["b"], envir=x) assign(map(x)["b"], 3, envir=x) ---%> x[c(1,3)] <- list(2, "Hello world!") x y <- as.list(x) str(y) z <- as.listenv(y) z identical(z, x) all.equal(z, x) })%> ``` ## Creating list environments List environments are created similarly to lists but also similarly to environments. To create an empty list environment, use ```r <%=withCapture({ x <- listenv() x })%> ``` This can later can be populated using named assignments, ```r <%=withCapture({ x$a <- 1 x })%> ``` comparable to how both lists and environments work. Similarly to lists, they can also be populated using indices, e.g. ```r <%=withCapture({ x[[2]] <- 2 x$c <- 3 x })%> ``` Just as for lists, a list environment is expanded with `NULL` elements whenever a new element is added that is beyond the current length plus one, e.g. ```r <%=withCapture({ x[[5]] <- 5 x x[[4]] })%> ``` As with lists, the above list environment can also be created from the start, e.g. ```r <%=withCapture({ x <- listenv(a=1, 3, c=4, NULL, 5) x })%> ``` As for lists, the length of a list environment can at any time be increased or decreased by assigning it a new length. If decreased, elements are dropped, e.g. ```r <%=withCapture({ x length(x) <- 2 x x[[1]] x[[2]] })%> ``` If increased, new elements are populated with unnamed elements of `NULL`, e.g. ```r <%=withCapture({ length(x) <- 4 x x[[3]] x[[4]] })%> ``` To allocate an "empty" list environment (with all `NULL`:s) of a given length, do ```r <%=withCapture({ x <- listenv() length(x) <- 4 x })%> ``` _Note_: Unfortunately, it is _not_ possible to use `x <- vector("listenv", length=4)`; that construct is only supported for the basic data types. ## Iterating over elements ### Iterating over elements by names Analogously to lists and plain environments, it is possible to iterate over elements of list environments by the element names. For example, ```r <%=withCapture({ x <- listenv(a=1, b=2, c=3) for (name in names(x)) { cat(sprintf("Element %s: %s\n", sQuote(name), x[[name]])) } })%> ``` ### Iterating over elements by indices Analogously to lists, but contrary to plain environments, it is also possible to iterate over elements by their indices. For example, ```r <%=withCapture({ x <- listenv(a=1, b=2, c=3) for (ii in seq_along(x)) { cat(sprintf("Element %d: %s\n", ii, x[[ii]])) } })%> ``` ## Coercion to and from list environments ### Coercing to lists and vectors Coercing a list environment to a list: ```r <%=withCapture({ x <- listenv(a=2, b=3, c="hello") x y <- as.list(x) str(y) })%> ``` Coercing a list to a list environment: ```r <%=withCapture({ z <- as.listenv(y) z identical(z, x) all.equal(z, x) })%> ``` Unlisting: ```r <%=withCapture({ unlist(x) unlist(x[-3]) unlist(x[1:2], use.names=FALSE) })%> ``` ## Multi-dimensional list environments Analogously to lists, and contrary to plain environments, list environments can have dimensions with corresponding names. For example, ```r <%=withCapture({ x <- as.listenv(1:6) dim(x) <- c(2,3) dimnames(x) <- list(c("a", "b"), c("A", "B","C")) x })%> ``` An easy way to quickly get an overview is to coerce to a list, e.g. ```r <%=withCapture({ as.list(x) })%> ``` Individual elements of a list environment can be accessed using standard subsetting syntax, e.g. ```r <%=withCapture({ x[["a", "B"]] x[[1, 2]] x[[1, "B"]] })%> ``` We can assign individual elements similarly, e.g. ```r <%=withCapture({ x[["b", "B"]] <- -x[["b", "B"]] as.list(x) })%> ``` We can also assign multiple elements through dimensional subsetting, e.g. ```r <%=withCapture({ x[2,-1] <- 98:99 as.list(x) x["a",c(1,3)] <- list(97, "foo") as.list(x) x[] <- 1:6 as.list(x) })%> ``` Concurrently with dimensional names it is possible to have names of the invidual elements just as for list environments without dimensions. For example, ```r <%=withCapture({ names(x) <- letters[seq_along(x)] x x[["a"]] x[["f"]] x[c("a", "f")] unlist(x) })%> ``` Contrary to lists, element names are preserved also with multi-dimensional subsetting, e.g. ```r <%=withCapture({ x[1,2] x[1,2,drop=FALSE] x[1:2,2:1] x[2,] x[2,,drop=FALSE] x["b",-2,drop=FALSE] })%> ``` Note, whenever dimensions are set using `dim(x) <- dims` both the dimensional names and the element names are removed, e.g. ```r > dim(x) <- NULL > names(x) NULL ``` This behavior is by design, cf. `help("dim", package="base")`. <%--- Because of this, the listenv package provides the `undim()` function, which removes the dimensions but preserves the names, e.g. ```r <%=withCapture({ x <- undim(x) names(x) })%> ``` _Warning_: Since list environments _and their attributes_ are mutable, calling ```r undim(x) ``` will have the same effect as ```r x <- undim(x) ``` That is, the dimension attributes of `x` will be changed. The reason for this is explained in Section 'Important about environments' above. ---%> ### Limitations The current implementation does _not_ support _dimensional subsetting_ of more than one element. For instance, `x[1,]` is not supported by this version. ## Important about environments List environments are as their name suggests _environments_. Whenever working with environments in R, it is important to understand that _environments are mutable_ whereas all other of the basic data types in R are immutable. For example, consider the following function that assigns zero to element `a` of object `x`: ```r <%=withCapture({ setA <- function(x) { x$a <- 0 x } })%> ``` If we pass a regular list to this function, ```r <%=withCapture({ x <- list(a=1) y <- setA(x) x$a y$a })%> ``` we see that `x` is unaffected by the assignment. This is because _lists are immutable_ in R. However, if we pass an environment instead, ```r <%=withCapture({ x <- new.env() x$a <- 1 y <- setA(x) x$a y$a })%> ``` we find that `x` was affected by the assignment. This is because _environments are mutable_ in R. Since list environments inherits from environments, this also goes for them, e.g. ```r <%=withCapture({ x <- listenv(a=1) y <- setA(x) x$a y$a })%> ``` What is also important to understand is that it is not just the _content_ of an environment that is mutable but also its _attributes_. For example, ```r <%=withCapture({ x <- listenv(a=1) y <- x attr(y, "foo") <- "Hello!" attr(x, "foo") })%> ``` [listenv]: http://cran.r-project.org/package=listenv --- Copyright Henrik Bengtsson, 2015 listenv/tests/ 0000755 0001762 0000144 00000000000 12640067030 013076 5 ustar ligges users listenv/tests/undim.R 0000644 0001762 0000144 00000001177 12640067030 014343 0 ustar ligges users library("listenv") message("*** undim() ...") ## General x <- c(a=1, b=2, A=3, B=4) names <- names(x) dim <- c(2,2) dimnames <- list(c("a", "b"), c("A", "B")) ## Basic arrays y <- array(x, dim=dim, dimnames=dimnames) names(y) <- names z <- undim(y) stopifnot(identical(names(z), names)) ## Lists y <- as.list(x) dim(y) <- dim dimnames(y) <- dimnames names(y) <- names z <- undim(y) stopifnot(identical(names(z), names)) ## List environments y <- as.listenv(x) dim(y) <- dim dimnames(y) <- dimnames names(y) <- names z <- undim(y) stopifnot(identical(names(z), names)) message("*** undim() ... DONE") listenv/tests/utils.R 0000644 0001762 0000144 00000002224 12640067030 014361 0 ustar ligges users printf <- function(...) cat(sprintf(...)) hpaste <- listenv:::hpaste # Some vectors x <- 1:6 y <- 10:1 z <- LETTERS[x] # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Abbreviation of output vector # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - printf("x = %s.\n", hpaste(x)) ## x = 1, 2, 3, ..., 6. printf("x = %s.\n", hpaste(x, maxHead=2)) ## x = 1, 2, ..., 6. printf("x = %s.\n", hpaste(x), maxHead=3) # Default ## x = 1, 2, 3, ..., 6. # It will never output 1, 2, 3, 4, ..., 6 printf("x = %s.\n", hpaste(x, maxHead=4)) ## x = 1, 2, 3, 4, 5 and 6. # Showing the tail printf("x = %s.\n", hpaste(x, maxHead=1, maxTail=2)) ## x = 1, ..., 5, 6. # Turning off abbreviation printf("y = %s.\n", hpaste(y, maxHead=Inf)) ## y = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 ## ...or simply printf("y = %s.\n", paste(y, collapse=", ")) ## y = 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Adding a special separator before the last element # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - # Change last separator printf("x = %s.\n", hpaste(x, lastCollapse=" and ")) ## x = 1, 2, 3, 4, 5 and 6. listenv/tests/parse_env_subset.R 0000644 0001762 0000144 00000015341 12640067030 016574 0 ustar ligges users library("listenv") ovars <- ls(envir=globalenv()) if (exists("x")) rm(list="x") if (exists("y")) rm(list="y") ## - - - - - - - - - - - - - - - - - - - - - - - - - - ## Variable in global/parent environment ## - - - - - - - - - - - - - - - - - - - - - - - - - - message("*** parse_env_subset() on parent environment ...") target <- parse_env_subset(x, substitute=TRUE) str(target) stopifnot(identical(target$envir, environment()), target$name == "x", is.na(target$idx), !target$exists) target <- parse_env_subset("x", substitute=TRUE) str(target) stopifnot(identical(target$envir, environment()), target$name == "x", is.na(target$idx), !target$exists) x <- NULL target <- parse_env_subset(x, substitute=TRUE) str(target) stopifnot(identical(target$envir, environment()), target$name == "x", is.na(target$idx), target$exists) target <- parse_env_subset(y, substitute=TRUE) str(target) stopifnot(identical(target$envir, environment()), target$name == "y", is.na(target$idx), !target$exists) message("*** parse_env_subset() on parent environment ... DONE") ## - - - - - - - - - - - - - - - - - - - - - - - - - - ## Environment ## - - - - - - - - - - - - - - - - - - - - - - - - - - message("parse_env_subset() on environment ...") x <- new.env() target <- parse_env_subset(x, substitute=TRUE) str(target) stopifnot(identical(target$envir, environment()), target$name == "x", is.na(target$idx), target$exists) target <- parse_env_subset(x$a, substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", is.na(target$idx), !target$exists) target <- parse_env_subset("a", envir=x, substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", is.na(target$idx), !target$exists) target <- parse_env_subset(x[["a"]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", is.na(target$idx), !target$exists) target <- parse_env_subset("a", envir=x, substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", is.na(target$idx), !target$exists) res <- try(target <- parse_env_subset(1, substitute=FALSE), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(target <- parse_env_subset(x[[1]], substitute=TRUE), silent=TRUE) stopifnot(inherits(res, "try-error")) x$a <- 1 target <- parse_env_subset(x$a, substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", is.na(target$idx), target$exists) message("parse_env_subset() on environment ... DONE") ## - - - - - - - - - - - - - - - - - - - - - - - - - - ## List environment ## - - - - - - - - - - - - - - - - - - - - - - - - - - message("*** parse_env_subset() on listenv ...") x <- listenv() target <- parse_env_subset(x, substitute=TRUE) str(target) stopifnot(identical(target$envir, environment()), target$name == "x", is.na(target$idx), target$exists) target <- parse_env_subset(x$a, substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", is.na(target$idx), !target$exists) target <- parse_env_subset(x[["a"]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", is.na(target$idx), !target$exists) target <- parse_env_subset("a", envir=x, substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", is.na(target$idx), !target$exists) target <- parse_env_subset(x[[1]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "", target$idx == 1, !target$exists) target <- parse_env_subset(x[[2]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "", target$idx == 2, !target$exists) x$a <- 1 target <- parse_env_subset(x$a, substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", target$idx == 1, target$exists) target <- parse_env_subset("a", envir=x, substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", target$idx == 1, target$exists) stopifnot(x$a == 1) stopifnot(x[[1]] == 1) target <- parse_env_subset(x[[1]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", target$idx == 1, target$exists) x[[3]] <- 3 target <- parse_env_subset(x[[3]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "", target$idx == 3, target$exists) stopifnot(x[[3]] == 3) print(names(x)) stopifnot(identical(names(x), c("a", "", ""))) b <- 1 target <- parse_env_subset(x[[b]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$name == "a", target$idx == 1, target$exists) x <- listenv() length(x) <- 2 target <- parse_env_subset(x[[1]], substitute=TRUE) str(target) stopifnot(!target$exists) target <- parse_env_subset(x[[2]], substitute=TRUE) str(target) stopifnot(!target$exists) target <- parse_env_subset(x[[3]], substitute=TRUE) str(target) stopifnot(!target$exists) stopifnot(length(x) == 2) x[[2]] <- 2 target <- parse_env_subset(x[[2]], substitute=TRUE) str(target) stopifnot(target$exists) x[[4]] <- 4 stopifnot(length(x) == 4) target <- parse_env_subset(x[[3]], substitute=TRUE) str(target) stopifnot(!target$exists) target <- parse_env_subset(x[1:5], substitute=TRUE) stopifnot(length(target$idx) == 5, all(target$idx == 1:5)) str(target) target <- parse_env_subset(x[integer(0L)], substitute=TRUE) stopifnot(length(target$idx) == 0) str(target) target <- parse_env_subset(x[[integer(0L)]], substitute=TRUE) stopifnot(length(target$idx) == 0) str(target) target <- parse_env_subset(x[0], substitute=TRUE) stopifnot(length(target$idx) == 0) str(target) target <- parse_env_subset(x[-1], substitute=TRUE) stopifnot(length(target$idx) == length(x)-1) str(target) ## Odds and ends target <- parse_env_subset(x[[""]], substitute=TRUE) stopifnot(length(target$idx) == 1L, !target$exists) message("*** parse_env_subset() on listenv ... DONE") ## - - - - - - - - - - - - - - - - - - - - - - - - - - ## Exception handling ## - - - - - - - - - - - - - - - - - - - - - - - - - - message("*** parse_env_subset() - exceptions ...") x <- listenv() res <- try(target <- parse_env_subset(x[[0]], substitute=TRUE), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(target <- parse_env_subset("_a", substitute=TRUE), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(target <- parse_env_subset(1:10, envir=x, substitute=FALSE), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(target <- parse_env_subset(c("a", "b"), envir=x, substitute=FALSE), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(target <- parse_env_subset(x@a, substitute=TRUE), silent=TRUE) stopifnot(inherits(res, "try-error")) message("*** parse_env_subset() - exceptions ... DONE") ## Cleanup rm(list=setdiff(ls(envir=globalenv()), ovars), envir=globalenv()) listenv/tests/get_variable,dimensions.R 0000644 0001762 0000144 00000001611 12640067030 020011 0 ustar ligges users library("listenv") ovars <- ls(envir=globalenv()) oopts <- options(warn=1) map <- listenv:::map message("* get_variable() - multi-dimensional list environments ...") x <- listenv() length(x) <- 6 dim(x) <- c(2,3) for (ii in seq_along(x)) { stopifnot(is.null(x[[ii]])) idx <- arrayInd(ii, .dim=dim(x)) stopifnot(is.null(x[[idx[1],idx[2]]])) varV <- get_variable(x, ii, create=FALSE) varA <- get_variable(x, idx, create=FALSE) stopifnot(identical(varA, varV)) } x[1:6] <- 1:6 for (ii in seq_along(x)) { stopifnot(identical(x[[ii]], ii)) idx <- arrayInd(ii, .dim=dim(x)) stopifnot(identical(x[[idx[1],idx[2]]], ii)) varV <- get_variable(x, ii) varA <- get_variable(x, idx) stopifnot(identical(varA, varV)) } message("* get_variable() - multi-dimensional list environments ... DONE") ## Cleanup options(oopts) rm(list=setdiff(ls(envir=globalenv()), ovars), envir=globalenv()) listenv/tests/get_variable.R 0000644 0001762 0000144 00000004445 12640067030 015654 0 ustar ligges users library("listenv") ovars <- ls(envir=globalenv()) oopts <- options(warn=1) map <- listenv:::map x <- listenv() length(x) <- 3L names(x) <- c("a", "b", "c") stopifnot(length(x) == 3L) print(map(x)) var <- get_variable(x, "a") stopifnot(!is.na(var)) stopifnot(length(x) == 3L) print(map(x)) var <- get_variable(x, "b") stopifnot(!is.na(var)) stopifnot(length(x) == 3L) print(map(x)) var <- get_variable(x, "c") stopifnot(!is.na(var)) stopifnot(length(x) == 3L) print(map(x)) var <- get_variable(x, "d") stopifnot(!is.na(var)) stopifnot(length(x) == 4L) print(map(x)) var <- get_variable(x, 4L) stopifnot(!is.na(var)) stopifnot(length(x) == 4L) print(map(x)) x$b <- 2 var <- get_variable(x, "b") stopifnot(!is.na(var)) stopifnot(length(x) == 4L) print(map(x)) var <- get_variable(x, length(x) + 1L) stopifnot(length(x) == 5L) print(names(x)) print(map(x)) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Allocation ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv() length(x) <- 3L print(x[[1]]) print(x[[2]]) print(x[[3]]) ## Out-of-bound subsetting res <- try(x[[0]], silent=TRUE) stopifnot(inherits(res, "try-error")) ## Out-of-bound subsetting res <- try(x[[4]], silent=TRUE) stopifnot(inherits(res, "try-error")) print(get_variable(x, 1L, mustExist=FALSE)) print(get_variable(x, 2L, mustExist=FALSE)) print(get_variable(x, 3L, mustExist=FALSE)) ## Out-of-bound element res <- try(var <- get_variable(x, 0L, mustExist=TRUE), silent=TRUE) stopifnot(inherits(res, "try-error")) ## Out-of-bound element res <- try(var <- get_variable(x, length(x) + 1L, mustExist=TRUE), silent=TRUE) stopifnot(inherits(res, "try-error")) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Exception handling ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv() length(x) <- 3L names(x) <- c("a", "b", "c") ## Non-existing element res <- try(var <- get_variable(x, "z", mustExist=TRUE), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(var <- get_variable(x, c("a", "b")), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(var <- get_variable(x, 1+2i), silent=TRUE) stopifnot(inherits(res, "try-error")) ## Cleanup options(oopts) rm(list=setdiff(ls(envir=globalenv()), ovars), envir=globalenv()) listenv/tests/listenv,dimensions.R 0000644 0001762 0000144 00000015307 12640067030 017060 0 ustar ligges users library("listenv") ovars <- ls(envir=globalenv()) oopts <- options(warn=1) message("* List environment and multiple dimensions ...") x <- listenv() dim(x) <- c(0,0) print(x) stopifnot(length(x) == 0) x <- listenv(a=1) stopifnot(identical(names(x), "a")) dim(x) <- c(1,1) print(x) stopifnot(length(x) == 1) stopifnot(is.null(dimnames(x))) stopifnot(is.null(names(x))) x0 <- as.list(1:6) x <- as.listenv(x0) print(x) stopifnot(is.null(dim(x))) stopifnot(is.null(dimnames(x))) y <- as.list(x) stopifnot(identical(y, x0)) z <- as.listenv(y) stopifnot(all.equal(z, x)) message("* dim(x) and dimnames(x) ...") dims <- list(2:3, 2:4) for (kk in seq_along(dims)) { dim <- dims[[kk]] dimnames <- lapply(dim, FUN=function(n) letters[seq_len(n)]) names <- letters[seq_len(prod(dim))] str(list(dim=dim, dimnames=dimnames, names=names)) n <- prod(dim) values <- seq_len(n) x0 <- as.list(values) x <- as.listenv(values) print(x) stopifnot(identical(dim(x), dim(x0))) y <- as.list(x) stopifnot(identical(y, x0)) z <- as.listenv(y) stopifnot(all.equal(z, x)) dim(x0) <- dim dim(x) <- dim print(x) stopifnot(identical(dim(x), dim(x0))) stopifnot(is.null(dimnames(x))) stopifnot(is.null(names(x))) names(x0) <- names names(x) <- names y <- as.list(x) stopifnot(identical(y, x0)) z <- as.listenv(y) stopifnot(all.equal(z, x)) excls <- c(list(NULL), as.list(seq_along(dimnames)), list(seq_along(dimnames))) for (ll in seq_along(excls)) { excl <- excls[[ll]] dimnamesT <- dimnames dimnamesT[excl] <- list(NULL) dimnames(x0) <- dimnamesT dimnames(x) <- dimnamesT print(x) stopifnot(identical(dim(x), dim(x0))) stopifnot(identical(dimnames(x), dimnames(x0))) stopifnot(identical(names(x), names)) y <- as.list(x) stopifnot(identical(y, x0)) z <- as.listenv(y) stopifnot(all.equal(z, x)) } ## for (ll ...) } ## for (kk ...) # Assign names x <- as.listenv(1:6) dim(x) <- c(2,3) dimnames(x) <- lapply(dim(x), FUN=function(n) letters[seq_len(n)]) names(x) <- letters[seq_along(x)] print(x) stopifnot(!is.null(dim(x))) stopifnot(!is.null(dimnames(x))) stopifnot(!is.null(names(x))) stopifnot(x[["b"]] == 2L) stopifnot(x[["a", "b"]] == 3L) ## Extract single element message("* y <- x[[i,j]] and z <- x[i,j] ...") dim(x) <- c(2,3) dimnames(x) <- list(c("a", "b"), NULL) y <- x[[3]] stopifnot(identical(y, 3L)) z <- x[3] stopifnot(identical(z[[1]], y)) y <- x[[1,1]] stopifnot(identical(y, x[[1]])) z <- x[1,1] stopifnot(identical(z[[1]], y)) y <- x[[2,3]] stopifnot(identical(y, x[[6]])) z <- x[2,3] stopifnot(identical(z[[1]], y)) y <- x[["a",3]] stopifnot(identical(y, x[[1,3]])) stopifnot(identical(y, x[[5]])) z <- x["a",3] stopifnot(identical(z[[1]], y)) y <- x[[1,c(FALSE,FALSE,TRUE)]] stopifnot(identical(y, x[[1,3]])) stopifnot(identical(y, x[[5]])) z <- x[1,c(FALSE,FALSE,TRUE)] stopifnot(identical(z[[1]], y)) message("* x[[i,j]] <- value ...") ## Assign single element x[[3]] <- -x[[3]] stopifnot(identical(x[[3]], -3L)) x[[1,1]] <- -x[[1,1]] stopifnot(identical(x[[1]], -1L)) x[[2,3]] <- -x[[2,3]] stopifnot(identical(x[[6]], -6L)) x[["a",3]] <- -x[["a",3]] stopifnot(identical(x[[1,3]], -5L)) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Multi-element subsetting ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - message("* x[i], x[i,j] ...") x <- as.listenv(1:24) dim(x) <- c(2,3,4) names(x) <- letters[seq_along(x)] x[2] <- list(NULL) print(x) y <- x[] print(y) stopifnot(length(y) == length(x)) stopifnot(all.equal(y, x)) stopifnot(!identical(y, x)) stopifnot(all.equal(as.list(y), as.list(x)[])) y <- x[1] print(y) stopifnot(all.equal(as.list(y), as.list(x)[1])) y <- x[2:3] print(y) stopifnot(all.equal(as.list(y), as.list(x)[2:3])) y <- x[-1] print(y) stopifnot(all.equal(as.list(y), as.list(x)[-1])) y <- x[1:2,1:3,1:4] print(y) stopifnot(all.equal(dim(y), dim(x))) stopifnot(all.equal(y, x)) stopifnot(all.equal(unlist(y), unlist(x))) stopifnot(all.equal(as.list(y), as.list(x)[1:2,1:3,1:4], check.attributes=FALSE)) y <- x[0,0,0] print(y) stopifnot(length(y) == 0) stopifnot(all.equal(dim(y), c(0,0,0))) stopifnot(all.equal(y, as.list(x)[0,0,0])) y <- x[0,,] print(y) stopifnot(length(y) == 0) stopifnot(all.equal(dim(y), c(0,dim(x)[-1]))) stopifnot(all.equal(y, as.list(x)[0,,])) y <- x[2,1,,drop=FALSE] print(y) stopifnot(all.equal(dim(y), c(1,1,dim(x)[3]))) stopifnot(all.equal(as.list(y), as.list(x)[2,1,,drop=FALSE], check.attributes=FALSE)) y <- x[2,1,,drop=TRUE] print(y) stopifnot(is.null(dim(y))) stopifnot(all.equal(as.list(y), as.list(x)[2,1,,drop=TRUE], check.attributes=FALSE)) y <- x[2,1,] print(y) stopifnot(is.null(dim(y))) stopifnot(all.equal(as.list(y), as.list(x)[2,1,], check.attributes=FALSE)) y <- x[-1,,c(3,3,1)] print(y) stopifnot(all.equal(as.list(y), as.list(x)[-1,,c(3,3,1)], check.attributes=FALSE)) message("* x[i], x[i,j] ... DONE") message("* x[i] <- value, x[i,j] <- value ...") dim <- c(2,3) n <- prod(dim) names <- letters[seq_len(n)] x0 <- as.list(1:n) dim(x0) <- dim names(x0) <- names x <- as.listenv(1:n) dim(x) <- dim names(x) <- names x0[] <- 6:1 x[] <- 6:1 stopifnot(all(unlist(x) == unlist(x0))) x0[1,] <- 1:3 x[1,] <- 1:3 stopifnot(all(unlist(x) == unlist(x0))) x0[,-2] <- 1:2 x[,-2] <- 1:2 stopifnot(all(unlist(x) == unlist(x0))) message("* x[i] <- value, x[i,j] <- value ... DONE") message("* Exceptions ...") x <- listenv() res <- try(dim(x) <- c(2,3), silent=TRUE) stopifnot(inherits(res, "try-error")) length(x) <- 6 dim(x) <- c(2,3) res <- try(x[[3,3]], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[3,3], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[c(-1,1),3], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[c(TRUE, TRUE, TRUE),], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(dimnames(x) <- NA, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(dimnames(x) <- list("a", "b", "c"), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(dimnames(x) <- list("a", NULL), silent=TRUE) stopifnot(inherits(res, "try-error")) dimnames(x) <- list(c("a", "b"), NULL) message("* Changing dim(x) and dimnames(x) ...") x <- listenv() x[1:12] <- 1:12 dim(x) <- c(2,2,3) dimnames(x) <- list(c("a", "b"), NULL, NULL) print(x) stopifnot(identical(dim(x), c(2L,2L,3L))) stopifnot(identical(dimnames(x), list(c("a", "b"), NULL, NULL))) x[[2,1,2]] <- -x[[2,1,2]] y <- unlist(x) print(y) dim(x) <- c(4,3) print(x) stopifnot(identical(dim(x), c(4L,3L))) stopifnot(is.null(dimnames(x))) x[[2,2]] <- -x[[2,2]] y <- unlist(x) print(y) stopifnot(identical(y, 1:12)) message("* List environment and multiple dimensions ... DONE") ## Cleanup options(oopts) rm(list=setdiff(ls(envir=globalenv()), ovars), envir=globalenv()) listenv/tests/parse_env_subset,dimensions.R 0000644 0001762 0000144 00000006543 12640067030 020745 0 ustar ligges users library("listenv") ovars <- ls(envir=globalenv()) if (exists("x")) rm(list="x") if (exists("y")) rm(list="y") ## - - - - - - - - - - - - - - - - - - - - - - - - - - ## Multi-dimensional subsetting ## - - - - - - - - - - - - - - - - - - - - - - - - - - message("*** parse_env_subset() on multi-dimensional listenv ...") x <- listenv() length(x) <- 6 dim(x) <- c(2,3) target <- parse_env_subset(x[[2]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$idx == 2, !target$exists) target <- parse_env_subset(x[[1,2]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$idx == 3, !target$exists) x[[1,2]] <- 1.2 target <- parse_env_subset(x[[1,2]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$idx == 3, target$exists) target <- parse_env_subset(x[[1,4]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), is.na(target$idx), !target$exists) ## Assert that x[[1,4]] is not the same as x[[c(1,4)]] target <- parse_env_subset(x[[1,4]], substitute=TRUE) str(target) target2 <- parse_env_subset(x[[c(1,4)]], substitute=TRUE) str(target2) target$code <- target2$code <- NULL stopifnot(!isTRUE(all.equal(target2, target))) dimnames(x) <- list(c("a", "b"), c("A", "B", "C")) print(x) target <- parse_env_subset(x[["a",2]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$idx == 3, target$exists) target <- parse_env_subset(x[["a","B"]], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$idx == 3, target$exists) target <- parse_env_subset(x["a","B"], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), target$idx == 3, target$exists) target <- parse_env_subset(x["a",1:3], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), length(target$idx) == 3, all(target$idx == c(1,3,5))) target <- parse_env_subset(x["a",], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), length(target$idx) == 3, all(target$idx == c(1,3,5))) target <- parse_env_subset(x["a",-1], substitute=TRUE) str(target) stopifnot(identical(target$envir, x), length(target$idx) == 2, all(target$idx == c(3,5))) message("*** parse_env_subset() on multi-dimensional listenv ... DONE") ## - - - - - - - - - - - - - - - - - - - - - - - - - - ## Exception handling ## - - - - - - - - - - - - - - - - - - - - - - - - - - message("*** parse_env_subset() on multi-dimensional listenv - exceptions ...") x <- listenv() ## Multidimensional subsetting on 'x' without dimensions res <- try(target <- parse_env_subset(x[[1,2]], substitute=TRUE), silent=TRUE) stopifnot(inherits(res, "try-error")) ## Multi-dimensional subsetting x <- listenv() length(x) <- 6 dim(x) <- c(2,3) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - ## FIXME: Should zero indices give parse errors or not? ## - - - - - - - - - - - - - - - - - - - - - - - - - - - res <- try(target <- parse_env_subset(x[[0]], substitute=TRUE), silent=TRUE) ## stopifnot(inherits(res, "try-error")) res <- try(target <- parse_env_subset(x[[1,0]], substitute=TRUE), silent=TRUE) ## stopifnot(inherits(res, "try-error")) res <- try(target <- parse_env_subset(x[[1,2,3]], substitute=TRUE), silent=TRUE) ## stopifnot(inherits(res, "try-error")) message("*** parse_env_subset() on multi-dimensional listenv - exceptions ... DONE") ## Cleanup rm(list=setdiff(ls(envir=globalenv()), ovars), envir=globalenv()) listenv/tests/listenv.R 0000644 0001762 0000144 00000035607 12640067030 014720 0 ustar ligges users library("listenv") ovars <- ls(envir=globalenv()) oopts <- options(warn=1) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Allocation ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv() print(x) stopifnot(length(x) == 0) stopifnot(is.null(names(x))) x <- listenv(a=1) print(x) stopifnot(length(x) == 1) stopifnot(identical(names(x), c("a"))) stopifnot(identical(x$a, 1)) x <- listenv(a=1, b=2:3) print(x) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("a", "b"))) stopifnot(identical(x$a, 1), identical(x$b, 2:3)) x <- listenv(b=2:3, .a=1) print(x) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("b", ".a"))) stopifnot(identical(x$.a, 1), identical(x$b, 2:3)) x <- listenv(length=3, a=1) print(x) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("length", "a"))) stopifnot(identical(x$length, 3), identical(x$a, 1)) withCallingHandlers({ x <- listenv(length=3) }, warning = function(warn) { cat("WARNING:", warn$message) invokeRestart("muffleWarning") }) print(x) stopifnot(length(x) == 3) stopifnot(is.null(names(x))) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Single-element assignments and subsetting ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv() print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 0) x$a <- 1 print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 1) stopifnot(identical(names(x), c("a"))) stopifnot(identical(x$a, 1), is.null(x$b)) x$b <- 2 print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("a", "b"))) stopifnot(identical(x$b, 2)) x$a <- 0 print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("a", "b"))) stopifnot(identical(x[["a"]], 0)) x$"a" <- 1 print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("a", "b"))) stopifnot(identical(x$a, 1)) x[["a"]] <- 0 print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("a", "b"))) key <- "b" x[[key]] <- 3 print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("a", "b"))) stopifnot(identical(x$b, 3), identical(x[["b"]], 3), identical(x[[key]], 3)) x[[3]] <- 3.14 print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 3) stopifnot(identical(names(x), c("a", "b", ""))) stopifnot(identical(x[[3]], 3.14)) names(x) <- c("a", "b", "c") stopifnot(length(x) == 3) stopifnot(identical(names(x), c("a", "b", "c"))) stopifnot(identical(x[[3]], 3.14), identical(x[["c"]], 3.14), identical(x$c, 3.14)) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Multi-element subsetting ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Assert than no false names are introduced x <- listenv() x[1:3] <- list(1, NULL, 3) print(x) stopifnot(is.null(names(x))) y <- x[] print(y) stopifnot(length(y) == length(x)) stopifnot(all.equal(y, x)) stopifnot(!identical(y, x)) stopifnot(is.null(names(y))) y <- x[1] print(y) stopifnot(is.null(names(y))) y <- x[2:3] print(y) stopifnot(is.null(names(y))) y <- x[-1] print(y) stopifnot(is.null(names(y))) x[c('c', '.a', 'b')] <- list(NULL, 3, 1) print(x) stopifnot(identical(names(x), c("", "", "", "c", ".a", "b"))) y <- as.list(x) str(y) stopifnot(identical(names(y), c("", "", "", "c", ".a", "b"))) y <- as.list(x, all.names=FALSE) str(y) stopifnot(identical(names(y), c("", "", "", "c", "b"))) y <- as.list(x, sorted=TRUE) str(y) stopifnot(identical(names(y), c("", "", "", ".a", "b", "c"))) y <- as.list(x, all.names=FALSE, sorted=TRUE) str(y) stopifnot(identical(names(y), c("", "", "", "b", "c"))) x <- listenv() x[c('a', 'b', 'c')] <- list(1, NULL, 3) y <- x[NULL] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list())) y <- x[integer(0L)] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list())) y <- x["a"] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list(a=1))) y <- x[c("a","c")] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list(a=1, c=3))) y <- x[c("c","a")] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list(c=3, a=1))) y <- x[c(1,3)] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list(a=1, c=3))) y <- x[-2] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list(a=1, c=3))) y <- x[-c(1,3)] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list(b=NULL))) y <- x[rep(1L, times=6L)] print(y) z <- as.list(y) print(z) stopifnot(identical(z, rep(list(a=1), times=6L))) y <- x[1:10] print(y) z <- as.list(y) print(z) stopifnot(identical(z, c(as.list(x), rep(list(NULL), times=7L)))) y <- x[c(TRUE, FALSE, TRUE)] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list(a=1, c=3))) y <- x[c(TRUE, FALSE)] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list(a=1, c=3))) y <- x[TRUE] print(y) z <- as.list(y) print(z) stopifnot(identical(z, as.list(x))) y <- x[FALSE] print(y) z <- as.list(y) print(z) stopifnot(identical(z, list())) y <- x[rep(TRUE, times=5L)] print(y) z <- as.list(y) print(z) stopifnot(identical(z, c(as.list(x), list(NULL), list(NULL)))) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Local access ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv(a=1, b=2, c=3.14) y <- local({ x[[3]] }) stopifnot(identical(y, 3.14)) y <- local({ x[3] }) stopifnot(identical(y[[1]], 3.14)) y <- local({ ii <- 3 x[[ii]] }) stopifnot(identical(y, 3.14)) y <- local({ ii <- 3 x[ii] }) stopifnot(identical(y[[1]], 3.14)) local({ x[[3]] <- 42L }) y <- x[[3]] stopifnot(identical(y, 42L)) local({ x[3] <- 3.14 }) y <- x[[3]] stopifnot(identical(y, 3.14)) local({ ii <- 3 x[ii] <- 42L }) y <- x[[3]] stopifnot(identical(y, 42L)) local({ ii <- 3 x[[ii]] <- 3.14 }) y <- x[[3]] stopifnot(identical(y, 3.14)) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Removing elements ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x[["a"]] <- NULL print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("b", "c"))) x[[3L]] <- NULL print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("b", "c"))) x[[2L]] <- NULL print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 1) stopifnot(identical(names(x), c("b"))) x$b <- NULL print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 0) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Assigning NULL ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x[2L] <- list(NULL) print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("", ""))) x['c'] <- list(NULL) print(x) print(length(x)) print(names(x)) stopifnot(length(x) == 3) stopifnot(identical(names(x), c("", "", "c"))) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Assigning multiple elements at once ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv() x[c('a', 'b', 'c')] <- 1:3 print(x) str(as.list(x)) print(length(x)) print(names(x)) stopifnot(length(x) == 3) stopifnot(identical(names(x), c("a", "b", "c"))) stopifnot(identical(as.list(x), list(a=1L, b=2L, c=3L))) stopifnot(identical(unlist(x), c(a=1L, b=2L, c=3L))) x[] <- 3:1 stopifnot(length(x) == 3) stopifnot(identical(names(x), c("a", "b", "c"))) stopifnot(identical(as.list(x), list(a=3L, b=2L, c=1L))) x[c('c', 'b')] <- 2:3 print(x) str(as.list(x)) print(length(x)) print(names(x)) stopifnot(length(x) == 3) stopifnot(identical(names(x), c("a", "b", "c"))) stopifnot(identical(as.list(x), list(a=3L, b=3L, c=2L))) x[c('a', 'c')] <- 1L print(x) str(as.list(x)) print(length(x)) print(names(x)) stopifnot(length(x) == 3) stopifnot(identical(names(x), c("a", "b", "c"))) stopifnot(identical(as.list(x), list(a=1L, b=3L, c=1L))) x[c('d', 'e')] <- 4:5 print(x) str(as.list(x)) print(length(x)) print(names(x)) stopifnot(length(x) == 5) stopifnot(identical(names(x), c("a", "b", "c", "d", "e"))) stopifnot(identical(as.list(x), list(a=1L, b=3L, c=1L, d=4L, e=5L))) x <- listenv() x[c('a', 'b')] <- 1:2 x[c(TRUE,FALSE)] <- 2L print(x) str(as.list(x)) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("a", "b"))) stopifnot(identical(as.list(x), list(a=2L, b=2L))) x[c(TRUE)] <- 1L print(x) str(as.list(x)) print(length(x)) print(names(x)) stopifnot(length(x) == 2) stopifnot(identical(names(x), c("a", "b"))) stopifnot(identical(as.list(x), list(a=1L, b=1L))) x[c(TRUE,FALSE,TRUE,FALSE)] <- 3L print(x) str(as.list(x)) print(length(x)) print(names(x)) stopifnot(length(x) == 3) stopifnot(identical(names(x), c("a", "b", ""))) stopifnot(identical(as.list(x), list(a=3L, b=1L, 3L))) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Expanding ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv() for (ii in 1:3) { x[[ii]] <- letters[ii] print(x[[ii]]) } print(x) names(x) <- sprintf("item%d", seq_along(x)) print(x) y <- as.list(x) str(y) stopifnot(identical(names(y), c("item1", "item2", "item3"))) stopifnot(identical(y[[1]], "a"), identical(y[[2]], "b"), identical(y[[3]], "c")) x[[2]] <- "B" stopifnot(identical(x$item2, "B")) x <- listenv() x[[1]] <- { 1 } x[[3]] <- { "Hello world!" } stopifnot(length(x) == 3) stopifnot(identical(seq_along(x), seq_len(length(x)))) print(x) names(x) <- c("a", "b", "c") print(x) x$b <- TRUE stopifnot(identical(x[[1]], 1)) stopifnot(identical(x[[2]], TRUE)) stopifnot(identical(x$b, TRUE)) stopifnot(identical(x[["b"]], TRUE)) y <- as.list(x) str(y) stopifnot(length(y) == 3) ## Mixed names and indices x <- listenv() x$a <- 1 x[[3]] <- 3 print(names(x)) stopifnot(identical(names(x), c("a", "", ""))) # First element (should be named 'a') var <- get_variable(x, "a") stopifnot(var == "a") var <- get_variable(x, 1) stopifnot(var == "a") # Third element (should be a temporary name) var <- get_variable(x, 3) stopifnot(var != "c") names(x) <- c("a", "b", "c") var <- get_variable(x, 3) stopifnot(var != "c") var <- get_variable(x, "c") stopifnot(var != "c") ## Second element (should become 'b', because it was never used # before it was "named" 'b') x$b <- 2 var <- get_variable(x, 2) stopifnot(var == "b") var <- get_variable(x, "b") stopifnot(var == "b") ## Names where as.integer(names(x)) are integers x <- listenv() x[["1"]] <- 1 x[["3"]] <- 3 print(names(x)) stopifnot(identical(names(x), c("1", "3"))) ## Expand and shrink x <- listenv() stopifnot(length(x) == 0L) length(x) <- 3L stopifnot(length(x) == 3L) stopifnot(is.null(names(x))) names(x) <- c("a", "b", "c") x$a <- 2 stopifnot(identical(x$a, 2)) x[c("a", "c")] <- c(2,1) stopifnot(identical(x$a, 2), identical(x$c, 1)) length(x) <- 4L stopifnot(length(x) == 4L) stopifnot(identical(names(x), c("a", "b", "c", ""))) length(x) <- 1L stopifnot(length(x) == 1L) stopifnot(identical(names(x), c("a"))) stopifnot(identical(x$a, 2)) length(x) <- 0L stopifnot(length(x) == 0L) stopifnot(length(names(x)) == 0) ## Actually, character(0), cf. lists ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Flatten ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - for (recursive in c(FALSE, TRUE)) { x <- list(); x$a <- list(B=1:3); x$b <- list(C=1:3, D=4:5) y1 <- unlist(x, recursive=recursive) x <- listenv(); x$a <- list(B=1:3); x$b <- list(C=1:3, D=4:5) y2 <- unlist(x, recursive=recursive) stopifnot(identical(y2, y1)) } # for (recursive ...) x <- listenv(); x$a <- list(B=1:3); x$b <- as.listenv(list(C=1:3, D=4:5)) y3 <- unlist(x, recursive=TRUE) stopifnot(identical(y3, y1)) x <- listenv() y <- unlist(x) stopifnot(length(y) == 0) stopifnot(is.null(y)) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Comparisons ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv(c=NULL, .a=3, b=1) print(x) ## A list environment is always equal to itself stopifnot(all.equal(x, x)) ## List environments emulate lists stopifnot(all.equal(x, list(c=NULL, .a=3, b=1))) stopifnot(all.equal(x, list(c=NULL, .a=3, b=1), sorted=TRUE)) stopifnot(all.equal(x, list(.a=3, b=1, c=NULL), sorted=TRUE)) stopifnot(all.equal(x, list(c=NULL, b=1), all.names=FALSE)) stopifnot(all.equal(x, list(.a=3, c=NULL, b=1), all.names=FALSE)) stopifnot(all.equal(x, list(b=1, c=NULL), all.names=FALSE, sorted=TRUE)) res <- all.equal(x, list(b=1, c=NULL), sorted=FALSE) stopifnot(!isTRUE(res)) res <- all.equal(x, list(b=1, c=NULL), all.names=FALSE) stopifnot(!isTRUE(res)) ## Assert listenv() -> as.list() -> as.listenv() equality y <- as.list(x) stopifnot(identical(names(y), names(x))) z <- as.listenv(y) stopifnot(identical(names(z), names(y))) stopifnot(all.equal(x, y)) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Warnings ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv() x[1:3] <- 1:3 res <- tryCatch(x[1:2] <- 1:4, warning=function(w) { class(w) <- "try-warning" w }) stopifnot(inherits(res, "try-warning")) res <- tryCatch(x[1:3] <- 1:2, warning=function(w) { class(w) <- "try-warning" w }) stopifnot(inherits(res, "try-warning")) res <- tryCatch(x[integer(0L)] <- 1, warning=function(w) { class(w) <- "try-warning" w }) stopifnot(!inherits(res, "try-warning")) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Exception handling ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- listenv() length(x) <- 3L names(x) <- c("a", "b", "c") res <- try(names(x) <- c("a", "b"), silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[1:2]], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[0]], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[length(x)+1]], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[1+2i]], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[1+2i], silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[1+2i]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[1+2i] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[integer(0L)]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[1:2]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[Inf]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[0]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[-1]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[character(0L)]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[c("a", "b")]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) res <- try(x[[""]] <- 1, silent=TRUE) stopifnot(inherits(res, "try-error")) ## Cleanup options(oopts) rm(list=setdiff(ls(envir=globalenv()), ovars), envir=globalenv()) listenv/tests/as.listenv.R 0000644 0001762 0000144 00000001332 12640067030 015306 0 ustar ligges users library("listenv") ovars <- ls(envir=globalenv()) oopts <- options(warn=1) ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - ## Single-element assignments and subsetting ## - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - x <- list(a=1, b=2, c=3) str(x) y <- as.listenv(x) print(y) stopifnot(identical(as.list(y), x)) z <- as.listenv(y) stopifnot(identical(as.list(y), as.list(z))) e <- new.env() e$a <- 1 e$b <- 2 e$c <- 3 y <- as.listenv(e) print(y) stopifnot(identical(as.list(y), as.list(e))) x <- c(a=1, b=2, c=3) y <- as.listenv(x) print(y) stopifnot(identical(as.list(y), as.list(x))) ## Cleanup options(oopts) rm(list=setdiff(ls(envir=globalenv()), ovars), envir=globalenv()) listenv/NAMESPACE 0000644 0001762 0000144 00000001505 12640067030 013154 0 ustar ligges users # Generated by roxygen2: do not edit by hand S3method("$",listenv) S3method("$<-",listenv) S3method("[",listenv) S3method("[<-",listenv) S3method("[[",listenv) S3method("[[<-",listenv) S3method("dim<-",listenv) S3method("dimnames<-",listenv) S3method("length<-",listenv) S3method("names<-",listenv) S3method(all.equal,listenv) S3method(as.list,listenv) S3method(as.listenv,default) S3method(as.listenv,environment) S3method(as.listenv,list) S3method(as.listenv,listenv) S3method(dim,listenv) S3method(dimnames,listenv) S3method(get_variable,listenv) S3method(length,listenv) S3method(names,listenv) S3method(print,listenv) S3method(undim,default) S3method(undim,listenv) S3method(unlist,listenv) export(as.listenv) export(get_variable) export(listenv) export(map) export(parse_env_subset) export(undim) listenv/NEWS 0000644 0001762 0000144 00000003714 12640067030 012440 0 ustar ligges users Package: listenv ================ Version: 0.6.0 [2015-12-27] o Added support for multi-dimensional subsetting of list environments just as for list. o BUG FIX: parse_env_subset(x[[idx]]) for list environment 'x' and index 'idx' claimed x[[idx]] exists as long as idx in [1,length(x)] but it forgot to check if element really existed, which may not be true if 'x' has been expanded. Version: 0.5.0 [2015-10-30] o Add support for assigning elements when creating list environment similarly how to lists work, e.g. x <- listenv(a=1, b=2). o length(x) <- n now expand/truncate a list environment. o Added unlist() and all.equal() for list environments. o DEPRECATED: Deprecated x <- listenv(length=n). Instead use x <- listenv(); length(x) <- n. o BUG FIX: as.listenv(x) would drop NULL elements in 'x'. o BUG FIX: x[idxs], x[name] <- y and x$