Learn R Programming

container (version 0.3.0)

Container: A sequence container

Description

This class implements a container data structure with typical member functions to insert, delete and access objects from the container. It also serves as the base class for Deque, Set, and Dict.

Usage

Container

Arguments

Format

An object of class R6ClassGenerator of length 24.

R6 constructor

Container$new(x=list())

Container methods

add(elem)

Add elem to Container.

apply(f)

Apply function f to all elements and return results in a list)

clear()

Remove all elements from the Container.

discard(elem, right=FALSE)

Search for first elem in Container and, if found, remove it. If right is TRUE, search from right to left.

empty()

Return TRUE if the Container is empty, else FALSE.

has(elem)

Return TRUE if Container contains elem else FALSE.

print(list.len)

Print object representation similar to str

remove(elem, right=FALSE)

Same as discard, but throw an error if not found.

size()

Return size of the Container.

type()

Return type (or mode) of internal vector containing the elements.

values()

Return a copy of all elements in the same format as they are stored in the object.

Details

The underlying data structure is based on R vectors (or lists), with the mode being set to the mode (or type) of the value passed to the initialize function, which by default is an empty list, in which case the Container object can store objects of mixed and arbitrary types. If the container will only contain objects of one particular type, for example, double values, it will be both more efficient and type safe to initialize the container using this particular type (see Examples section).

See Also

Iterable, Deque, Set, and Dict

Examples

Run this code
# NOT RUN {
c0 <- Container$new()
c0$size()                            # 0
c0$add(1)
c0$add(2)$add("A")                   # chaining example
c0$has(2)                            # TRUE
c0$discard(2)$has(2)                 # FALSE

# }
# NOT RUN {
c0$remove(2)                         # Error : 2 not in Container
# }
# NOT RUN {
c0$discard(2)$has(2)                 # still FALSE, but no error

# Container types
Container$new(list("A", 1))$type()   # "list"
Container$new(numeric(0))$type()     # "double"
Container$new(0+0i)$type()           # "complex"
Container$new(letters[1:3])$type()   # "character"
Container$new(letters[1:3])$values() # "a" "b" "c"
Container$new(1L)$type()             # "integer"
Container$new(1L)$add(2.3)$values()  # since integer type, equals c(1, 2)
# }

Run the code above in your browser using DataLab