flow: Create Flow objects to better organize pipelines in R

View source: R/flow.R

flowR Documentation

Create Flow objects to better organize pipelines in R

Description

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.

Usage

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 class 'Flow'
x$name

## S3 replacement method for class 'Flow'
x$name <- value

Arguments

.

If a Flow object is provided, inherit from it, otherwise, create a new Flow object inheriting from .GlobalEnv with . as pipe value.

.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.

env

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.

objects

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

x

An object (a Flow object, or anything to test if it is a Flow object in is_flow()).

name

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.

value

The value or expression to assign to name inside the Flow object.

Details

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.

See Also

str.Flow, quos_underscore, %>_%

Examples

library(svFlow)
library(dplyr)
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)

foo(iris, x_ = Petal.Width)

foo(iris, x_ = Petal.Width, fun_ = median)
# 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
foo2(1:10) -> foo_obj
ls(foo_obj)

SciViews/flow documentation built on Sept. 9, 2023, 10:22 a.m.