bind: Bind a (piped) object to a symbol for complex function...

View source: R/pipebind.R

bindR Documentation

Bind a (piped) object to a symbol for complex function evaluation

Description

The base R |> pipe lacks some advanced functionality compared to the {magrittr} ⁠%>%⁠ pipe. For example, the piped object can only appear once on the right-hand side of the pipe (either as the first unnamed argument or elsewhere using the ⁠_⁠ placeholder in R 4.2.0 and later), and the ⁠_⁠ placeholder cannot appear on the left side of sub-setting functions like $, [, [[, or @.

The bind() function is a way to conveniently circumvent these limitations. Pipe an object into bind(), choose a placeholder symbol to represent it, then use this placeholder to refer the piped object in any way and as many times as desired in an R expression.

The Greek letter λ() is available as an alias for bind().

Usage

bind(.pipeValue, .pipeBind, ...)

Arguments

.pipeValue

The object to bind. Typically specified by piping into the bind() function (e.g., x |> bind()).

.pipeBind

The placeholder symbol to use to represent the piped object. Can be any valid R object name.

...

An R expression. Any valid R code (expression).

Value

The results of the expression, evaluated using the piped object.

Examples

# Piping to a non-first argument
mtcars |>
  transform(kmL = mpg / 2.35) |>
  bind(d, lm(kmL ~ hp, data = d))

# Using the piped value multiple times
rnorm(10, mean = 10) |>
  bind(x, x - mean(x))

# Using the piped value in multiple arguments
c(a = 1, b = 2, c = 3) |>
  bind(x, paste(names(x), x, sep = " = "))

# Subsetting the piped value
mtcars |>
  bind(d, d$mpg)

pipebind documentation built on Aug. 31, 2023, 1:08 a.m.