This work is funded by the National Science Foundation grant NSF-IOS 1546858.
library(rmonad) library(magrittr) set.seed(210)
rmonad
offers
a stateful pipeline framework
pure error handling
access to the intermediate results of a pipeline
effects -- e.g. plotting, caching -- within a pipeline
branching and chaining of pipelines
a flexible approach to literate programming
I will introduce rmonad
with a simple sequence of squares
# %>>% corresponds to Haskell's >>= 1:5 %>>% sqrt %>>% sqrt %>>% sqrt
So what exactly did rmonad
do with your data? It is still there, sitting
happily inside the monad.
In magrittr
you could do something similar:
1:5 %>% sqrt %>% sqrt %>% sqrt
%>%
takes the value on the left and applies it to the function on the right.
%>>%
, takes a monad on the left and a function on the right, then builds a
new monad from them. This new monad holds the computed value, if the
computation succeeded. It collates all errors, warnings, and messages. These
are stored in step-by-step a history of the pipeline.
%>%
is an application operator, %>>%
is a monadic bind operator.
magrittr
and rmonad
complement each other. %>%
can be used inside a
monadic sequence to perform operations on monads, whereas %>>%
performs
operations in them. If this is all too mystical, just hold on, you don't
need to understand monads to understand the examples.
Below, we store an intermediate value in the monad:
1:5 %>>% sqrt %v>% # store this result sqrt %>>% sqrt
The %v>%
variant of the monadic bind operator stores the results as they
are passed.
Following the example of magrittr
, arbitrary anonymous functions of '.' are
supported
1:5 %>>% { o <- . * 2 ; { o + . } %>% { . + o } }
Warnings are caught and stored
-1:3 %>>% sqrt %v>% sqrt %>>% sqrt
Similarly for errors
"wrench" %>>% sqrt %v>% sqrt %>>% sqrt
The first sqrt
failed, and this step was coupled to the resultant error.
Contrast this with magrittr
, where the location of the error is lost:
"wrench" %>% sqrt %>% sqrt %>% sqrt
Also note that a value was still produced. This value will never be used in the downstream monadic sequence (except when explicitly doing error handling). However it, and all other information in the monad, can be easily accessed.
rmonad
If you want to extract the terminal result from the monad, you can use the esc
function:
1:5 %>>% sqrt %>% esc
esc
is our first example of a class of functions that work on monads, rather
than the values they wrap. We use magrittr
's application operator %>%
here,
rather than the monadic bind operator %>>%
, because we are passing a literal
monad to esc
.
If the monad is in a failed state, esc
will raise an error.
"wrench" %>>% sqrt %>>% sqrt %>% esc
If you prefer a tabular summary of your results, you can pipe the monad into
the mtabulate
function.
1:5 %>>% sqrt %v>% sqrt %>>% sqrt %>% mtabulate
An internal states can be accessed by converting the monad to a list of past states and simple indexing out the ones you want.
All errors, warnings and notes can be extracted with the missues
command
-2:2 %>>% sqrt %>>% colSums %>% missues
The id
column refers to row numbers in the mtabulate
output. Internal
values can be extracted:
result <- 1:5 %v>% sqrt %v>% sqrt %v>% sqrt get_value(result)[[2]]
The %>_%
operator is useful when you want to include a function inside a
pipeline that should be bypassed, but you want the errors, warnings, and
messages to pass along with the main.
You can cache an intermediate result
cars %>_% write.csv(file="cars.tab") %>>% summary
Or plot a value along with a summary
cars %>_% plot(xlab="index", ylab="value") %>>% summary
I pipe the final monad into forget
, which is (like esc
) a function for
operating on monads. forget
removes history from a monad. I do this just to
de-clutter the output.
You can call multiple effects
cars %>_% plot(xlab="index", ylab="value") %>_% write.csv(file="cars.tab") %>>% summary
Since state is passed, you can make assertions about the data inside a pipeline.
iris %>_% { stopifnot(is.data.frame(.)) } %>_% { stopifnot(sapply(.,is.numeric)) } %>>% colSums %|>% head
The above code will enter a failed state if the input is either not a data
frame or the columns are not all numeric. The braced expressions are anonymous
functions of '.' (as in magrittr
). The final expression %|>%
catches an
error and performs head
on the last valid input (iris
).
Errors needn't be viewed as abnormal. For example, we might want to try several alternatives functions, and use the first that works.
1:10 %>>% colSums %|>% sum
Here we will do either colSums
or sum
. The pipeline fails only if both
fail.
Sometimes you want to ignore the previous failure completely, and make a new call -- for example in reading files:
# try to load a cached file, on failure rerun the analysis read.table("analyasis_cache.tab") %||% run_analysis(x)
This can also be used to replace if-else if-else strings
x <- list() # compare if(length(x) > 0) { x[[1]] } else { NULL } # to x[[1]] %||% NULL %>% esc
Or maybe you want to support multiple extensions for an input file
read.table("a.tab") %||% read.table("a.tsv") %>>% dostuff
Used together with %|>%
we can build full error handling pipelines
letters[1:10] %v>% colSums %|>% sum %||% message("Can't process this")
Overall, in rmonad
, errors are well-behaved. It is reasonable to write
functions that return an error rather than one of the myriad default values
(NULL
, NA
, logical(0)
, list()
, FALSE
). This approach is unambiguous.
rmonad
can catch the error and allow allow the programmer to deal with it
accordingly.
If you want to perform an operation on a value inside the chain, but don't want
to pass it, you can use the branch operator %>^%
.
rnorm(30) %>^% qplot(xlab="index", ylab="value") %>>% mean
This stores the result of qplot
in a branch off the main pipeline. This means
that plot
could fail, but the rest of the pipeline could continue. You can
store multiple branches.
rnorm(30) %>^% qplot(xlab="index", ylab="value") %>^% summary %>>% mean
Branches can be used as input, as well.
x <- 1:10 %>^% dgamma(10, 1) %>^% dgamma(10, 5) %^>% cor get_value(x)
Note the branches could be long monadic chains themselves, which might have their own branches.
Use of the %>^%
and %^>%
operators is a little awkward. A more general
option is to use tags and views. tag
this allows the head of a pipeline to
be reset.
# build memory cacher f <- make_recacher(memory_cache) # make core dataset m <- evalwrap(iris) %>>% dplyr::select( sepal_length = Sepal.Length, sepal_width = Sepal.Width, species = Species ) %>% # cache value with tag 'iris' f('iris') %>>% # some downstream stuff nrow # Now can pick from the tagged node m <- view(m, 'iris') %>>% { qplot( x=sepal_length, y=sepal_width, color=species, data=. )} %>% f('plot') # and repeat however many times we like m <- view(m, 'iris') %>>% summary %>% f('sum') plot(m)
If you want to connect many chains, all with independent inputs, you can do so
with the %__%
operator.
runif(10) %>>% sum %__% rnorm(10) %>>% sum %__% rexp(10) %>>% sum
The %__%
operator records the output of the lhs and evaluates the rhs into an
rmonad
. This operator is a little like a semicolon, in that it demarcates
independent statements. Each statement, though, is wrapped into a graph of
operations. This graph is itself data, and can be computed on. You could take
any analysis and recompose it as %__%
delimited blocks. The result of running
the analysis would be a data structure containing all results and errors.
program <- { x = 2 y = 5 x * y } %__% { letters %>% sqrt } %__% { 10 * x }
You can link chunks of code, with their results, and performance information.
So far our pipelines have been limited to either linear paths or the somewhat
awkward branch merging. An easier approach is to read inputs from a list. But
we want to be able to catch errors resulting from evaluation of each member of
the list. We can do this with list_meval
.
funnel( "yolo", stop("stop, drop, and die"), runif("simon"), k = 2 )
This returns a monad which fails if any of the components evaluate to an error. But it does not toss the rest of the inputs, instead returning a clean list with a NULL filling in missing pieces. Contrast this with normal list evaluation:
list( "yolo", stop("stop, drop, and die"), runif("simon"), 2)
funnel
records each failure in each element of the list independently.
This approach can also be used with the infix operator %*>%
.
funnel(read.csv("a.csv"), read.csv("b.csv")) %*>% merge
Now, of course, we can add monads to the mix
funnel( a = read.csv("a.csv") %>>% do_analysis_a, b = read.csv("b.csv") %>>% do_analysis_b, k = 5 ) %*>% joint_analysis
Monadic list evaluation is the natural way to build large programs from smaller pieces.
As our pipelines become more complex, it becomes essential to document them. We can do that as follows:
{ "This is docstring. The following list is metadata associated with this node. Both the docstring and the metadata list will be processed out of this function before it is executed. They also will not appear in the code stored in the Rmonad object." list(sys = sessionInfo(), foo = "This can be anything") # This NULL is necessary, otherwise the metadata list above would be # treated as the node output NULL } %__% # The %__% operator connects independent pieces of a pipeline. "a" %>>% { "The docstrings are stored in the Rmonad objects. They may be extracted in the generation of reports. For example, they could go into a text block below the code in a knitr document. The advantage of having documentation here, is that it is coupled unambiguously to the generating function. These annotations, together with the ability to chain chains of monads, allows whole complex workflows to be built, with the results collated into a single object. All errors propagate exactly as errors should, only affecting downstream computations. The final object can be converted into a markdown document and automatically generated function graphs." paste(., "b") }
rmonad
pipelines may be nested to arbitrary depth.
foo <- function(x, y) { "This is a function containing a pipeline. It always fails" "a" %>>% paste(x) %>>% paste(y) %>>% log } bar <- function(x) { "this is another function, it doesn't fail" funnel("b", "c") %*>% foo %>>% paste(x) } "d" %>>% bar
This function descends through three levels of nesting. There is a failure at
the deepest level. This failing node, where a string is passed to a log
function, stores the error message and the input. Each node ascending from the
point of failure stores their respective input. This allows debugging to resume
from any desired level.
A feature new to rmonad v0.4
are a set of post-processors. These act on an
Rmonad
object after the code the object wraps has been evaluated.
Here are the currently supported post-processors:
format_warnings
- A function of the final value and the list of warnings,
that formats the node's warning message.
format_error
- Like format_warnings
but for errors
format_notes
- Like format_warnings
but for messages/notes
summarize
- A function of the final value that stores a summary of the data
cache
- A function of the final value that caches the value
format_log
- A function of the final state that prints an progress message
These are all quite experimental at this point.
The post-processors are included in the node metadata, for example
"hello world" %>>% { list( format_error=function(x, err){ paste0("Failure on input '", x, "': ", err) } ) sqrt(.) }
summarize
is useful since it is often useful to store information about an
intermediate step but storing the full data is too memory intensive. Rather
than stopping the flow of an analysis with a bunch of intermediate analytic
code, a summary function can be nested in a node that holds an arbitrary
description of the data, coupled immediately to the function that produced it.
d <- mtcars %>>% { list(summarize=summary) subset(., mpg > 20) } %>>% nrow get_summary(d)[[2]]
The summary information will tucked away invisibly in the Rmonad
object until
a debugger or report generator extracts it. Of course, this could also be used
to just store a full copy of the output in memory, by setting the summarize
function to identity
.
Summaries like this will be more useful in the rmonad
world when a Shiny app
(or something comparable) makes the workflow graph interactive. Then the
summary for a node can automatically be displayed when the node is accessed.
The cache
and log
post-processors are not yet well developed. But they are
intended to do what their names suggest. cache
is not yet useful since I
don't have the infrastructure to test whether the cache is valid. log
will
eventually allow progress messages to be passed to STDOUT as rmonad
is
running (by default messages are captured and stored).
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.