Debian Codegraph @

R/pipe.R

raw
1 #' @importFrom magrittr %>%
2 #' @export
3 magrittr::"%>%"
4
5 #' @importFrom magrittr %T>%
6 #' @export
7 magrittr::"%T>%"
8
9
10 #' Promise pipe operators
11 #'
12 #' With R 4.1, the promise pipe operators are `r lifecycle::badge('superseded')` by [`promises::then`], [`promises::catch`], and
13 #' [`promises::finally`] methods when used in tandem with the function shorthand (`\(x) rhs(x)`) and `|>`.
14 #'
15 #' Promise-aware pipe operators, in the style of [magrittr](https://CRAN.R-project.org/package=magrittr/vignettes/magrittr.html).
16 #' Like magrittr pipes, these operators can be used to chain together pipelines
17 #' of promise-transforming operations. Unlike magrittr pipes, these pipes wait
18 #' for promise resolution and pass the unwrapped value (or error) to the `rhs`
19 #' function call.
20 #'
21 #' The `>` variants are for handling successful resolution, the `!` variants are
22 #' for handling errors. The `T` variants of each return the lhs instead of the
23 #' rhs, which is useful for pipeline steps that are used for side effects
24 #' (printing, plotting, saving).
25 #'
26 #' 1. \code{promise \%...>\% func()} is equivalent to \code{promise \%>\% then(func)}.
27 #' 2. \code{promise \%...!\% func()} is equivalent to \code{promise \%>\% catch(func)}.
28 #' 3. \code{promise \%...T>\% func()} is equivalent to \code{promise \%T>\% then(func)}.
29 #' 4. \code{promise \%...T!\% func()} is equivalent to \code{promise \%T>\%
30 #' catch(func)} or \code{promise \%>\% catch(func, tee = TRUE)}.
31 #'
32 #' One situation where 3. and 4. above break down is when `func()` throws an
33 #' error, or returns a promise that ultimately fails. In that case, the failure
34 #' will be propagated by our pipe operators but not by the
35 #' magrittr-plus-function "equivalents".
36 #'
37 #' For simplicity of implementation, we do not support the magrittr feature of
38 #' using a `.` at the head of a pipeline to turn the entire pipeline into a
39 #' function instead of an expression.
40 #'
41 #' @param lhs A promise object.
42 #' @param rhs A function call using the magrittr semantics. It can return either
43 #' a promise or non-promise value, or throw an error.
44 #'
45 #' @examples
46 #' \dontrun{
47 #' library(mirai)
48 #'
49 #' mirai(cars) %...>%
50 #' head(5) %...T>%
51 #' print()
52 #'
53 #' # If the read.csv fails, resolve to NULL instead
54 #' mirai(read.csv("http://example.com/data.csv")) %...!%
55 #' { NULL }
56 #' }
57 #'
58 #' @return A new promise.
59 #'
60 #' @seealso https://rstudio.github.io/promises/articles/promises_03_overview.html#using-pipes
61 #'
62 #' @name pipes
63 #' @export
64 `%...>%` <- function(lhs, rhs) {
65 # the parent environment
66 parent <- parent.frame()
67
68 # the environment in which to evaluate pipeline
69 env <- new.env(parent = parent)
70
71 parts <- match.call()
72 func <- pipeify_rhs(parts[[3L]], env)
73 then(lhs, func)
74 }
75
76 #' @rdname pipes
77 #' @export
78 `%...T>%` <- function(lhs, rhs) {
79 # the parent environment
80 parent <- parent.frame()
81
82 # the environment in which to evaluate pipeline
83 env <- new.env(parent = parent)
84
85 parts <- match.call()
86 func <- pipeify_rhs(parts[[3L]], env)
87 lhs %>%
88 then(func) %>%
89 then(function(value) lhs)
90 }
91
92 #' @rdname pipes
93 #' @export
94 `%...!%` <- function(lhs, rhs) {
95 # the parent environment
96 parent <- parent.frame()
97
98 # the environment in which to evaluate pipeline
99 env <- new.env(parent = parent)
100
101 parts <- match.call()
102 func <- pipeify_rhs(parts[[3L]], env)
103 catch(lhs, func)
104 }
105
106 #' @rdname pipes
107 #' @export
108 `%...T!%` <- function(lhs, rhs) {
109 # the parent environment
110 parent <- parent.frame()
111
112 # the environment in which to evaluate pipeline
113 env <- new.env(parent = parent)
114
115 parts <- match.call()
116 func <- pipeify_rhs(parts[[3L]], env)
117 catch(lhs, func, tee = TRUE)
118 }
119
120
121 has.visible <- function(func) {
122 ".visible" %in% names(formals(func))
123 }
124
125 pipeify_rhs <- function(rhs, env) {
126 if (is_parenthesized(rhs)) {
127 rhs <- eval(rhs, env, env)
128 }
129
130 rhs <- if (is_funexpr(rhs)) {
131 rhs
132 } else if (is_lambda(rhs)) {
133 # We can remove this conditional if we want this behavior to be supported.
134 # The next conditional checks for is_lambda too, and does the right thing.
135 # Keeping the error for now in deference to magrittr pipe behavior.
136 stop("Anonymous functions must be parenthesized")
137 } else if (is_function(rhs) || is_colexpr(rhs) || is_lambda(rhs)) {
138 # This block diverges from magrittr because we have an optional .visible
139 # argument that can be passed to the function. If the function takes a
140 # parameter called .visible then we will pass it, otherwise no.
141
142 real_rhs <- if (is.function(rhs)) {
143 rhs
144 } else {
145 eval(rhs, env, env)
146 }
147
148 if (has.visible(real_rhs)) {
149 as.call(list(rhs, quote(.), .visible = quote(.visible)))
150 } else {
151 as.call(list(rhs, quote(.)))
152 }
153 } else if (is_first(rhs)) {
154 prepare_first(rhs)
155 } else {
156 rhs
157 }
158
159 eval(call("function", as.pairlist(alist(. = , .visible = )), rhs), env, env)
160 }