Producer-class: Class defining methods for all Producers

Description Usage Arguments Methods Internal Class Fields and Methods Author(s) See Also Examples

Description

A virtual class representing components that can read data from connections, and yield records to the user or a Consumer instance. A Producer represents a source of data, responsible for parsing a file or other data source into records to be passed to Consumer classes. Producer and Consumer instances are associated with each other through the Stream function.

Usage

1
2
3
4
5
## S4 method for signature 'Producer'
lapply(X, FUN, ...)

## S4 method for signature 'Producer'
sapply(X, FUN, ..., simplify=TRUE, USE.NAMES=TRUE)

Arguments

X

An instance of class Producer

FUN

A function to be applied to each successful yield() of X.

...

Additional arguments to FUN.

simplify

See ?base::sapply.

USE.NAMES

See ?base::sapply but note that names do not usually make sense for instances of class Producer.

Methods

Methods defined on this class include:

Stream

Construct a stream from one Producer and one or more Consumer. See ?Stream.

yield

Yield a single result (e.g., data.frame) from the Producer.

reset

Reset, if possible, the Producer.

lapply, sapply

Apply FUN to each result applied to yield(), simplifying (using simplify2array) if possible for sapply. Partial results on error can be recovered using tryCatch, as illustrated below. Infinite producers will of course exhaust memory.

Internal Class Fields and Methods

Internal fields of this class are are described with, e.g., getRefClass("Producer")$fields.

Internal methods of this class are described with getRefClass("Producer")$methods() and getRefClass("Producer")$help().

Author(s)

Martin Morgan mtmorgan@fhcrc.org

See Also

Streamer-package, Consumer-class, Streamer-class.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
showClass("Producer")
showMethods(class="Producer", where="package:Streamer")

sapply(Seq(to=47, yieldSize=7), function(elt) {
    c(n = length(elt), xbar = mean(elt))
})

## recover partial results
fun = function(i) if (i == 5) stop("oops, i == 5") else i
res <- tryCatch(sapply(Seq(to=10), fun), error=function(err) {
    warning(conditionMessage(err),
            "\n  only partial results available")
    simplify2array(err$partialResult)
})
res

Example output

Class "Producer" [package "Streamer"]

Slots:
                  
Name:       .xData
Class: environment

Extends: 
Class "Streamer", directly
Class "envRefClass", by class "Streamer", distance 2
Class ".environment", by class "Streamer", distance 3
Class "refClass", by class "Streamer", distance 3
Class "environment", by class "Streamer", distance 4, with explicit coerce
Class "refObject", by class "Streamer", distance 4

Known Subclasses: 
Class "ConnectionProducer", directly
Class "Seq", directly
Class "FunctionProducer", directly
Class "ScanProducer", by class "ConnectionProducer", distance 2
Class "ReadLinesProducer", by class "ConnectionProducer", distance 2
Class "ReadTableProducer", by class "ConnectionProducer", distance 2
Class "RawInput", by class "ConnectionProducer", distance 2
Function: Stream (package Streamer)
x="Producer"

Function: lapply (package BiocGenerics)
X="Producer"

Function: sapply (package BiocGenerics)
X="Producer"

     [,1] [,2] [,3] [,4] [,5] [,6] [,7]
n       7    7    7    7    7    7    5
xbar    4   11   18   25   32   39   45
Warning message:
In value[[3L]](cond) : yield(): oops, i == 5
  only partial results available
[1] 1 2 3 4

Streamer documentation built on Nov. 8, 2020, 5:53 p.m.