bind | R Documentation |
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()
.
bind(.pipeValue, .pipeBind, ...)
.pipeValue |
The object to bind. Typically specified by piping into the
|
.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). |
The results of the expression, evaluated using the piped object.
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.