iarray: Create an iterator over an array

Description Usage Arguments See Also Examples

View source: R/iarray.R

Description

Create an iterator over an array. It is similar to the iapply function from the iterators package, but it has special support for nested foreach loops.

Usage

1
2
iarray(X, MARGIN, ..., chunks, chunkSize, drop,
       idx=lapply(dim(X), function(i) TRUE), quote=FALSE)

Arguments

X

Array to iterate over.

MARGIN

Vector of subscripts to iterate over. Note that if the length of MARGIN is greater than one, the resulting iterator will generate iterators which is particularly useful with nested foreach loops.

...

Used to force subsequent arguments to be specified by name.

chunks

Number of elements that the iterator should generate. This can be a single value or a vector the same length as MARGIN. A single value will be recycled for each dimension if MARGIN has more than one value.

chunkSize

The maximum size Number of elements that the iterator should generate. This can be a single value or a vector the same length as MARGIN. A single value will be recycled for each dimension if MARGIN has more than one value.

drop

Should dimensions of length 1 be dropped in the generated values? It defaults to FALSE if either chunks or chunkSize is specified, otherwise to TRUE.

idx

List of length length(dim(X)) containing indices used to create the expression that generates the iteration values. The default value contains all TRUE values, but you can use other values in order to iterate over a subset of X. See the example below for ways to do that.

quote

A logical value indicating whether the final iteration values should be quoted or not. This can be useful in a parallel context because it can be much more efficient to send call objects to parallel workers than the objects that they evaluate to.

See Also

apply, iapply

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  # Iterate over matrices in a 3D array
  x <- array(1:24, c(2,3,4))
  as.list(iarray(x, 3))

  # Iterate over subarrays
  as.list(iarray(x, 3, chunks=2))

  x <- array(1:64, c(4,4,4))
  it <- iarray(x, c(2,3), chunks=c(1,2))
  jt <- nextElem(it)
  nextElem(jt)
  jt <- nextElem(it)
  nextElem(jt)

  it <- iarray(x, c(2,3), chunks=c(2,2))
  jt <- nextElem(it)
  nextElem(jt)
  nextElem(jt)
  jt <- nextElem(it)
  nextElem(jt)
  nextElem(jt)

  # Use idx to iterate over only the middle four columns of a matrix
  x <- matrix(1:18, nrow=3, ncol=6)
  it <- iarray(x, 1, idx=list(TRUE, quote(2:5)))
  nextElem(it)
  nextElem(it)
  nextElem(it)

  # It can also be done using alist:
  it <- iarray(x, 1, idx=alist(, 2:5))
  nextElem(it)
  nextElem(it)
  nextElem(it)

  x <- matrix(1:1800, nrow=3, ncol=600)
  it <- iarray(x, 1, idx=alist(, 10:600), quote=TRUE)
  (yq <- nextElem(it))
  object.size(yq)
  object.size(eval(yq))

itertools documentation built on May 2, 2019, 6:16 p.m.