View source: R/reduceResults.R
reduceResults | R Documentation |
A version of Reduce
for Registry
objects
which iterates over finished jobs and aggregates them.
All jobs must have terminated, an error is raised otherwise.
reduceResults(fun, ids = NULL, init, ..., reg = getDefaultRegistry())
fun |
[ |
ids |
[ |
init |
[ |
... |
[ |
reg |
[ |
Aggregated results in the same order as provided ids.
Return type depends on the user function. If ids
is empty, reduceResults
returns init
(if available) or NULL
otherwise.
If you have thousands of jobs, disabling the progress bar (options(batchtools.progress = FALSE)
)
can significantly increase the performance.
Other Results:
batchMapResults()
,
loadResult()
,
reduceResultsList()
tmp = makeRegistry(file.dir = NA, make.default = FALSE)
batchMap(function(a, b) list(sum = a+b, prod = a*b), a = 1:3, b = 1:3, reg = tmp)
submitJobs(reg = tmp)
waitForJobs(reg = tmp)
# Extract element sum from each result
reduceResults(function(aggr, res) c(aggr, res$sum), init = list(), reg = tmp)
# Aggregate element sum via '+'
reduceResults(function(aggr, res) aggr + res$sum, init = 0, reg = tmp)
# Aggregate element prod via '*' where parameter b < 3
reduce = function(aggr, res, job) {
if (job$pars$b >= 3)
return(aggr)
aggr * res$prod
}
reduceResults(reduce, init = 1, reg = tmp)
# Reduce to data.frame() (inefficient, use reduceResultsDataTable() instead)
reduceResults(rbind, init = data.frame(), reg = tmp)
# Reduce to data.frame by collecting results first, then utilize vectorization of rbind:
res = reduceResultsList(fun = as.data.frame, reg = tmp)
do.call(rbind, res)
# Reduce with custom combine function:
comb = function(x, y) list(sum = x$sum + y$sum, prod = x$prod * y$prod)
reduceResults(comb, reg = tmp)
# The same with neutral element NULL
comb = function(x, y) if (is.null(x)) y else list(sum = x$sum + y$sum, prod = x$prod * y$prod)
reduceResults(comb, init = NULL, reg = tmp)
# Alternative: Reduce in list, reduce manually in a 2nd step
res = reduceResultsList(reg = tmp)
Reduce(comb, res)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.