Learn R Programming

iterators (version 1.0.2)

iter: Iterator Factory Functions

Description

iter is a generic function used to create iterator objects.

Usage

iter(obj, ...)

## S3 method for class 'default': iter(obj, checkFunc=function(...) TRUE, recycle=FALSE, ...) ## S3 method for class 'iter': iter(obj, \dots) ## 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

Run this code
# a vector iterator
  i1 <- iter(1:3)
  nextElem(i1)
  nextElem(i1)
  nextElem(i1)

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

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

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

  # a function iterator
  i4 <- iter(function() rnorm(1))
  nextElem(i4)
  nextElem(i4)
  nextElem(i4)

Run the code above in your browser using DataLab