inst/examples/Array.r

\dontrun{

##------------------------------------------------------------------------------
## Initialize
##------------------------------------------------------------------------------

## Default //
inst <- Array$new()
inst$.array

## Explicit //
inst <- Array$new(list(a = 1, b = 2))
inst$.array
inst <- Array$new(list(c = 1), list(d = 2))
inst$.array

## Atomic and mixed //
inst <- Array$new(a = 1, b = 2)
inst$.array
inst <- Array$new(a = 1, list(b = 2))
inst$.array

##------------------------------------------------------------------------------
## Add //
##------------------------------------------------------------------------------

## Single //
inst <- Array$new()
inst$add(list(a = 1))
inst$.array
inst$add(list(b = 2))
inst$.array

inst$add(list(b = 2), dups = FALSE)
inst$.array
try(inst$add(list(b = 2), dups = FALSE, strict = 1))
inst$.array
try(inst$add(list(b = 2), dups = FALSE, strict = 2))
inst$.array

inst$add(list(b = 2))
inst$.array

## Single atomic //
inst <- Array$new()
inst$add(1)
inst$.array
inst$add(new.env())
inst$.array
inst$add(a = new.env())
inst$.array
inst$add(b = 1)
inst$.array

## Multiple //
inst <- Array$new()
inst$add(list(a = 1), list(b = 2))
inst$.array

inst$add(1, 1, 1, id = c("c", "d", "e"))
inst$.array

inst$add(1, 1, 1, id = c("e", "f"), dups = FALSE)
inst$.array

inst <- Array$new()
inst$add(1, 1, 1, id = c("a", "b"))
try(inst$add(1, 1, 1, id = c("a", "b"), strict = 1))
try(inst$add(1, 1, 1, id = c("a", "b"), strict = 2))

##------------------------------------------------------------------------------
## Set //
##------------------------------------------------------------------------------

## Single //
inst <- Array$new(list(a = 1))
inst$set(list(a = 2))
inst$.array
inst$set(list(b = 2))
try(inst$set(list(b = 2), strict = 1))
try(inst$set(list(b = 2), strict = 2))
inst$.array

## Multiple //
inst <- Array$new(list(a = 1, b = 1))
inst$set(list(a = 2), list(b = 2))
inst$.array

inst <- Array$new(list(a = 1, b = 1, c = 1))
inst$set(2, 2, 2, id = c("a", "b", "c"))
inst$.array

inst$set(2, 2, 2, id = c("a", "b", "d"))
inst$.array
try(inst$set(2, 2, 2, id = c("a", "b", "d"), strict = 1))
try(inst$set(2, 2, 2, id = c("a", "b", "d"), strict = 2))

## Different lengths //
inst$set(2, 2, 2, id = c("b", "c"))
try(inst$set(2, 2, 2, id = c("b", "c"), strict = 1))
try(inst$set(2, 2, 2, id = c("b", "c"), strict = 2))

## New (or does not have to exist) //
inst <- Array$new(list(a = 1))
inst$set(list(b = 1), must_exist = FALSE)
inst$.array
inst$set(list(c = 1, d = 1), must_exist = FALSE)
inst$.array

##------------------------------------------------------------------------------
## Get //
##------------------------------------------------------------------------------

inst <- Array$new()
inst$get()

inst <- Array$new(array = list(a = 1, b = 1))
inst$.array, list(a = 1, b = 1))
inst$get()

inst$get("a")
inst$get("a", inner = FALSE)
inst$get(c("a", "b"))
inst$get(c("a", "b"), inner = FALSE)
inst$get("c")
inst$get("c", inner = FALSE)
inst$get(c("a", "c"))
inst$get(c("a", "c"), inner = FALSE)

inst <- Array$new(list(a = 1))
inst$get("b")
try(inst$get("b", strict = 1))
try(inst$get("b", strict = 2))
inst$get(c("a", "b"))
try(inst$get(c("a", "b"), strict = 1))
try(inst$get(c("a", "b"), strict = 2))

##------------------------------------------------------------------------------
## Exists //
##------------------------------------------------------------------------------

inst <- Array$new(array = list(a = 1, b = 1))
inst$exists("a")
inst$exists(c("a", "b"))
inst$exists("c")

##------------------------------------------------------------------------------
## Index //
##------------------------------------------------------------------------------

inst <- Array$new(array = list(a = 1, b = 1))
inst$index("a")
inst$index(c("a", "b"))
inst$index("c")
try(inst$index("c", strict = 1))
try(inst$index("c", strict = 2))
inst$index(c("a", "c"))

inst$index("c", simplify = TRUE)
try(inst$index("c", simplify = TRUE, strict = 1))
try(inst$index("c", simplify = TRUE, strict = 2))
inst$index(c("a", "c"), simplify = TRUE)

##------------------------------------------------------------------------------
## Clear //
##------------------------------------------------------------------------------

inst <- Array$new(array = list(a = 1, b = 1))
inst$clear()
inst$.array

##------------------------------------------------------------------------------
## Remove //
##------------------------------------------------------------------------------

inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rm("a")
inst$exists("a")
inst$rm(c("b", "c"))
inst$rm("a")
inst$rm(c("a", "b"))
inst$add(list(a = 1))
inst$rm(c("a", "b"))
inst$rm("a")
try(inst$rm("a", strict = 1))
try(inst$rm("a", strict = 2))

##------------------------------------------------------------------------------
## Remove first //
##------------------------------------------------------------------------------

inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rmFirst()
inst$get()
inst$rmFirst(2)
inst$get()

inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rmFirst(4)
inst$get()
inst <- Array$new(array = list(a = 1, b = 1, c = 1))
try(inst$rmFirst(4, strict = 1))
inst <- Array$new(array = list(a = 1, b = 1, c = 1))
try(inst$rmFirst(4, strict = 2))
inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rmFirst(4, simplify = TRUE)
inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rmFirst(3, simplify = TRUE)
inst$get()

inst$rmFirst()
inst$get()
  
##------------------------------------------------------------------------------
## Remove last //
##------------------------------------------------------------------------------

inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rmLast()
inst$get()
inst$rmLast(2)
inst$get()

## Simplify //
inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rmLast(simplify = TRUE)
inst$get()
inst$rmLast(2, simplify = TRUE)
inst$get()

inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rmLast(4)
inst$get()
inst <- Array$new(array = list(a = 1, b = 1, c = 1))
try(inst$rmLast(4, strict = 1))
inst <- Array$new(array = list(a = 1, b = 1, c = 1))
try(inst$rmLast(4, strict = 2))

## Simplify //
inst <- Array$new(array = list(a = 1, b = 1, c = 1))
inst$rmLast(4, simplify = TRUE)
inst$get()

inst <- Array$new()
inst$rmLast()
try(inst$rmLast(strict = 1))
try(inst$rmLast(strict = 2))
inst$rmLast(2)
try(inst$rmLast(strict = 1))
try(inst$rmLast(strict = 2))
inst$get()

## Simplify //
inst <- Array$new()
inst$rmLast(simplify = TRUE)
try(inst$rmLast(strict = 1, simplify = TRUE))
inst$rmLast(2, simplify = TRUE)
try(inst$rmLast(strict = 1, simplify = TRUE))

##------------------------------------------------------------------------------
## Copy //
##------------------------------------------------------------------------------

inst <- Array$new(array = list(a = 1))
inst$copy("a", "b")
inst$get()

inst$copy("c", "d")
inst$get()
try(inst$copy("c", "d", strict = 1))
try(inst$copy("c", "d", strict = 2))

inst$copy(c("a", "b"), c("c", "d"))
inst$get()
inst$copy(c("a", "b"), c("c", "d"), dups = FALSE)
inst$get()
try(inst$copy(c("a", "b"), c("c", "d"), dups = FALSE, strict = 1))
try(inst$copy(c("a", "b"), c("c", "d"), dups = FALSE, strict = 2))
inst$copy(c("a", "b"), c("c", "d"))
inst$get()

inst$copy(c("a", "b"), "d")
try(inst$copy(c("a", "b"), "d", strict = 1))
try(inst$copy(c("a", "b"), "d", strict = 2))

}
rappster/arrayr documentation built on May 26, 2019, 11:09 p.m.