Learn R Programming

promises (version 1.3.2)

pipes: Promise pipe operators

Description

Promise-aware pipe operators, in the style of magrittr. Like magrittr pipes, these operators can be used to chain together pipelines of promise-transforming operations. Unlike magrittr pipes, these pipes wait for promise resolution and pass the unwrapped value (or error) to the rhs function call.

Usage

lhs %...>% rhs

lhs %...T>% rhs

lhs %...!% rhs

lhs %...T!% rhs

Value

A new promise.

Arguments

lhs

A promise object.

rhs

A function call using the magrittr semantics. It can return either a promise or non-promise value, or throw an error.

Details

The > variants are for handling successful resolution, the ! variants are for handling errors. The T variants of each return the lhs instead of the rhs, which is useful for pipeline steps that are used for side effects (printing, plotting, saving).

  1. promise %...>% func() is equivalent to promise %>% then(func).

  2. promise %...!% func() is equivalent to promise %>% catch(func).

  3. promise %...T>% func() is equivalent to promise %T>% then(func).

  4. promise %...T!% func() is equivalent to promise %T>% catch(func) or promise %>% catch(func, tee = TRUE).

One situation where 3. and 4. above break down is when func() throws an error, or returns a promise that ultimately fails. In that case, the failure will be propagated by our pipe operators but not by the magrittr-plus-function "equivalents".

For simplicity of implementation, we do not support the magrittr feature of using a . at the head of a pipeline to turn the entire pipeline into a function instead of an expression.

See Also

https://rstudio.github.io/promises/articles/overview.html#using-pipes

Examples

Run this code
if (FALSE) {
library(future)
plan(multisession)

future_promise(cars) %...>%
  head(5) %...T>%
  print()

# If the read.csv fails, resolve to NULL instead
future_promise(read.csv("http://example.com/data.csv")) %...!%
  { NULL }
}

Run the code above in your browser using DataLab