Flow objects, as explicitly created by flow()
, or implicitly by the
%>_%
pipe operator are proto objects (class-less objects
with possible inheritance) that can be combined nicely with pipelines using
the specialized flow pipe operator %>_%
(or by using $
).
They allow for encapsulating satellite objects/variables related to the
pipeline, and they deal with non-standard evaluations using the tidyeval
mechanism automatically with minimal changes required by the user.
flow(. = NULL, .value = NULL, ...)
enflow(.value, env = caller_env(), objects = ls(env))
is.flow(x)
is_flow(x)
as.flow(x, ...)
as_flow(x, ...)
# S3 method for Flow
$(x, name)
# S3 method for Flow
$(x, name) <- value
If a Flow object is provided, inherit from it, otherwise,
create a new Flow object inheriting from .GlobalEnv
with .
as pipe
value.
The pipe value to pass to the object (used instead of .
,
in case both are provided).
For flow()
, named arguments of other objects to create inside
the Flow object. If the name ends with _
, then, the expression is
automatically captured inside a quosure* (see quos_underscore()
).
For print()
, further arguments passed to the delegated object_print()
function (if it exists inside the Flow object), or to the print()
method of the object inside .value
.
The environment to use for populating the Flow object. All
objects from this environment are injected into it, with the objects not
starting with a dot and ending with an underscore (_
) automatically
converted into quosures
. The object provided to .value=
becomes the
default value of the Flow
object, that is, the data transferred to the
pipeline.
A character string with the name of the objects from env
to import into the Flow object. If env
is the calling environment (by
default), .value
is the name of an object, and that name appears in
objects
too, it is excluded from it to avoid importing it twice.
from that
An object (a Flow object, or anything to test if it is a
Flow object in is_flow()
).
The name of the item to get from a Flow object. If name
starts with two dots (..
), the item is searched in the Flow object
itself without inheritance, but the name is stripped of its leading two dots
first! If the content is a quosure, it is automatically unquoted, and for
the assignation version, if name ends with _
, the expression is
automatically converted into a quosure.
The value or expression to assign to name
inside the Flow
object.
enflow()
creates a Flow object in the head of a "flow pipeline" in the
context of a functional sequence, that is a function that converts an
ad hoc, single use pipeline into a function reusable in a different
context. Satellite data become arguments of the function.
When a Flow object is created from scratch, it always inherits
from .GlobalEnv
, no matter where the expression was executed (in fact, it
inherits from an empty root Flow object itself inheriting from
.GlobalEnv
). This is a deliberate design choice to overcome some
difficulties and limitations of proto objects, see proto()
.
enflow()
creates a Flow object and populates it automatically with all
the objects that are present in env=
(by default, the calling environment).
It is primarily intended to be used inside a function, as first instruction
of a "flow pipeline". Hence, it collects all function arguments inside that
pipeline in a most convenient way.
str.Flow, quos_underscore, %>_%
library(svFlow)
library(dplyr)
#>
#> Attaching package: ‘dplyr’
#> The following objects are masked from ‘package:stats’:
#>
#> filter, lag
#> The following objects are masked from ‘package:base’:
#>
#> intersect, setdiff, setequal, union
data(iris)
foo <- function(data, x_ = Sepal.Length, y_ = log_SL,
fun_ = mean, na_rm = TRUE)
enflow(data) %>_%
mutate(., y_ = log(x_)) %>_%
summarise(., fun_ = fun_(y_,
na.rm = na_rm_)) %>_% .
foo(iris)
#> mean
#> 1 1.755393
foo(iris, x_ = Petal.Width)
#> mean
#> 1 -0.1723227
foo(iris, x_ = Petal.Width, fun_ = median)
#> median
#> 1 0.2623643
# Unfortunately, this does not work, due to limitations of tidyeval's :=
#foo(iris, x_ = Petal.Width, fun_ = stats::median)
foo2 <- function(., x_ = Sepal.Length, y_ = log_SL, na_rm = TRUE)
enflow(.)
foo2
#> function(., x_ = Sepal.Length, y_ = log_SL, na_rm = TRUE)
#> enflow(.)
#> <environment: 0x562ed7efb048>
foo2(1:10) -> foo_obj
ls(foo_obj)
#> [1] "na_rm" "x" "y"