iter is a generic function used to create iterator objects.

iter(obj, ...)

# Default S3 method
iter(obj, checkFunc=function(...) TRUE, recycle=FALSE,
...)
# S3 method for class 'iter'
iter(obj, ...)
# S3 method for class 'matrix'
iter(obj, by=c('column', 'cell', 'row'), chunksize=1L,
checkFunc=function(...) TRUE, recycle=FALSE, ...)
# S3 method for class 'data.frame'
iter(obj, by=c('column', 'row'),
checkFunc=function(...) TRUE, recycle=FALSE, ...)
# S3 method for class 'function'
iter(obj, checkFunc=function(...) TRUE,
recycle=FALSE, ...)

Arguments

obj

an object from which to generate an iterator.

by

how to iterate.

chunksize

the number of elements of by to return with each call to nextElem.

checkFunc

a function which, when passed an iterator value, return TRUE or FALSE. If FALSE, the value is skipped in the iteration.

recycle

a boolean describing whether the iterator should reset after running through all it's values.

...

additional arguments affecting the iterator.

Value

The iterator.

Examples

  # a vector iterator
  i1 <- iter(1:3)
  nextElem(i1)
#> [1] 1
  nextElem(i1)
#> [1] 2
  nextElem(i1)
#> [1] 3

  # a vector iterator with a checkFunc
  i1 <- iter(1:3, checkFunc=function(i) i %% 2 == 0)
  nextElem(i1)
#> [1] 2

  # a data frame iterator by column
  i2 <- iter(data.frame(x=1:3, y=10, z=c('a', 'b', 'c')))
  nextElem(i2)
#> [1] 1 2 3
  nextElem(i2)
#> [1] 10 10 10
  nextElem(i2)
#> [1] "a" "b" "c"

  # a data frame iterator by row
  i3 <- iter(data.frame(x=1:3, y=10), by='row')
  nextElem(i3)
#>   x  y
#> 1 1 10
  nextElem(i3)
#>   x  y
#> 2 2 10
  nextElem(i3)
#>   x  y
#> 3 3 10

  # a function iterator
  i4 <- iter(function() rnorm(1))
  nextElem(i4)
#> [1] -0.8320195
  nextElem(i4)
#> [1] 1.100492
  nextElem(i4)
#> [1] -0.1738201