knitr::opts_chunk$set(echo=TRUE, comment=NA)
runr packs a set of higher order functions for running lists of functions in various modes.
:movie_camera: [runSeries]
:ocean: [runWaterfall]
:running: [runRace]
:100: [runParallel]
devtools::install_github('chiefbiiko/runr')
runr::run*(tasks = list(NULL), cb = NULL)
tasks List of functions required cb Callback with signature cb(error, data) optionalValues for the tasks or cb parameter can be defined anonymous or referenced to via a valid function name. If a callback is defined it will always have exactly one non-NULL argument only. Without errors during task execution the data argument of the callback is a named list. In case of errors during task execution the error argument of the callback is an ordinary error object with one additional property $task which indicates the function that threw.
# callback skeleton - must have exactly two parameters callback <- function(err, data) { if (!is.null(err)) stop(err, err$task) # check n go data }
bounds, a dependency of runr, has an export bounds::bind that allows binding parameters to functions. It takes a function and a variable sequence of parameters as inputs and returns a closure with the given parameters bound to it. Might come in handy at times.
runr::runSeries runs its input tasks sequentially returning either a named list (on error NULL) or the value of a given callback.
# a named function moo <- function() 'moooo' # run as series runr::runSeries(list(Sys.getpid, Sys.time, moo), callback) ``` *** ## runWaterfall `runr::runWaterfall` runs its input tasks sequentially, passing each task's return value to the next task, and returns either a named list (on error `NULL`) or the value of a given callback. :ocean: All tasks except the first must have at least one parameter. ```r # another named function zoo <- function() 1L:3L # bind and create a named closure reduceSum <- bounds::bind(Reduce, function(a, b) a + b) # chain/pipe consecutive returns runr::runWaterfall(list(zoo, factorial, reduceSum), callback)
runr::runRace runs its input tasks parallel, blocks until the very first return of any of its tasks and returns either a named list (all NULL but one; on error NULL) or the value of a given callback.
# two resembling workers dlHuckPDF <- bounds::bind(utils::download.file, 'http://contentserver.adobe.com/store/books/HuckFinn.pdf', 'huck_finn.pdf') dlHuckTXT <- bounds::bind(utils::download.file, 'http://www.textfiles.com/etext/AUTHORS/TWAIN/huck_finn', 'huck_finn.txt') # run a race runr::runRace(list(dlHuckPDF, dlHuckTXT), callback)
runr::runParallel runs its input tasks parallel, blocks until all complete and returns either a named list (on error NULL) or the value of a given callback.
# some stoopid workers d <- bounds::bind(jsonlite::fromJSON, 'https://api.github.com/users/chiefbiiko') o <- bounds::bind(base::sub, '^.*(3).*$', '\\1', paste0(installed.packages(), collapse='')) # a new callback cb <- function(err, data) { if (!is.null(err)) stop(err, err$task) sprintf('@%s | %s | <%s', data$d$login, grep('hi', names(data$d), value=TRUE), data$o) } # see ya! runr::runParallel(list(d, o), cb)
runRace, runParallel
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.