| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/pipe.R |
| 3 |
\name{pipes} |
| 4 |
\alias{pipes} |
| 5 |
\alias{\%...>\%} |
| 6 |
\alias{\%...T>\%} |
| 7 |
\alias{\%...!\%} |
| 8 |
\alias{\%...T!\%} |
| 9 |
\title{Promise pipe operators} |
| 10 |
\usage{ |
| 11 |
lhs \%...>\% rhs |
| 12 |
|
| 13 |
lhs \%...T>\% rhs |
| 14 |
|
| 15 |
lhs \%...!\% rhs |
| 16 |
|
| 17 |
lhs \%...T!\% rhs |
| 18 |
} |
| 19 |
\arguments{ |
| 20 |
\item{lhs}{A promise object.} |
| 21 |
|
| 22 |
\item{rhs}{A function call using the magrittr semantics. It can return either |
| 23 |
a promise or non-promise value, or throw an error.} |
| 24 |
} |
| 25 |
\value{ |
| 26 |
A new promise. |
| 27 |
} |
| 28 |
\description{ |
| 29 |
With R 4.1, the promise pipe operators are \ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#superseded}{\figure{lifecycle-superseded.svg}{options: alt='[Superseded]'}}}{\strong{[Superseded]}} by \code{\link{then}}, \code{\link{catch}}, and |
| 30 |
\code{\link{finally}} methods when used in tandem with the function shorthand (\verb{\\(x) rhs(x)}) and \verb{|>}. |
| 31 |
} |
| 32 |
\details{ |
| 33 |
Promise-aware pipe operators, in the style of \href{https://CRAN.R-project.org/package=magrittr/vignettes/magrittr.html}{magrittr}. |
| 34 |
Like magrittr pipes, these operators can be used to chain together pipelines |
| 35 |
of promise-transforming operations. Unlike magrittr pipes, these pipes wait |
| 36 |
for promise resolution and pass the unwrapped value (or error) to the \code{rhs} |
| 37 |
function call. |
| 38 |
|
| 39 |
The \code{>} variants are for handling successful resolution, the \code{!} variants are |
| 40 |
for handling errors. The \code{T} variants of each return the lhs instead of the |
| 41 |
rhs, which is useful for pipeline steps that are used for side effects |
| 42 |
(printing, plotting, saving). |
| 43 |
\enumerate{ |
| 44 |
\item \code{promise \%...>\% func()} is equivalent to \code{promise \%>\% then(func)}. |
| 45 |
\item \code{promise \%...!\% func()} is equivalent to \code{promise \%>\% catch(func)}. |
| 46 |
\item \code{promise \%...T>\% func()} is equivalent to \code{promise \%T>\% then(func)}. |
| 47 |
\item \code{promise \%...T!\% func()} is equivalent to \code{promise \%T>\% |
| 48 |
catch(func)} or \code{promise \%>\% catch(func, tee = TRUE)}. |
| 49 |
} |
| 50 |
|
| 51 |
One situation where 3. and 4. above break down is when \code{func()} throws an |
| 52 |
error, or returns a promise that ultimately fails. In that case, the failure |
| 53 |
will be propagated by our pipe operators but not by the |
| 54 |
magrittr-plus-function "equivalents". |
| 55 |
|
| 56 |
For simplicity of implementation, we do not support the magrittr feature of |
| 57 |
using a \code{.} at the head of a pipeline to turn the entire pipeline into a |
| 58 |
function instead of an expression. |
| 59 |
} |
| 60 |
\examples{ |
| 61 |
\dontrun{ |
| 62 |
library(mirai) |
| 63 |
|
| 64 |
mirai(cars) \%...>\% |
| 65 |
head(5) \%...T>\% |
| 66 |
print() |
| 67 |
|
| 68 |
# If the read.csv fails, resolve to NULL instead |
| 69 |
mirai(read.csv("http://example.com/data.csv")) \%...!\% |
| 70 |
{ NULL } |
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
\seealso{ |
| 75 |
https://rstudio.github.io/promises/articles/promises_03_overview.html#using-pipes |
| 76 |
} |