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
## 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().
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
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(Streamer)
> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/Streamer/Producer-class.Rd_%03d_medium.png", width=480, height=480)
> ### Name: Producer
> ### Title: Class defining methods for all Producers
> ### Aliases: Producer-class Producer lapply,Producer-method
> ### sapply,Producer-method
> ### Keywords: classes
>
> ### ** Examples
>
> showClass("Producer")
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
> showMethods(class="Producer", where="package:Streamer")
Function: Stream (package Streamer)
x="Producer"
Function: lapply (package BiocGenerics)
X="Producer"
Function: sapply (package BiocGenerics)
X="Producer"
>
> sapply(Seq(to=47, yieldSize=7), function(elt) {
+ c(n = length(elt), xbar = mean(elt))
+ })
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
n 7 7 7 7 7 7 5
xbar 4 11 18 25 32 39 45
>
> ## 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)
+ })
Warning message:
In value[[3L]](cond) : yield(): oops, i == 5
only partial results available
> res
[1] 1 2 3 4
>
>
>
>
>
> dev.off()
null device
1
>