Package 'pipebind'

Title: Flexible Binding for Complex Function Evaluation with the Base R |> Pipe
Description: Provides a simple function to bind a piped object to a placeholder symbol to enable complex function evaluation with the base R |> pipe.
Authors: Brenton M. Wiernik [cre, aut] (<https://orcid.org/0000-0001-9560-6336>, @bmwiernik)
Maintainer: Brenton M. Wiernik <[email protected]>
License: GPL-3
Version: 0.1.2.0001
Built: 2024-11-01 11:14:44 UTC
Source: https://github.com/bwiernik/pipebind

Help Index


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)

Pipe-able aliases

Description

pipebind provides several aliases for unary/binary operators (e.g., +) and replacement functions (e.g., ⁠names<-()⁠) that facilitate using these functions in a ⁠|>⁠ chain.

Some unary/binary operators cannot currently be used with the ⁠|>⁠ pipe, such as +, -, or %*%. These aliases provide a way to use these functions with the ⁠|>⁠ pipe.

Currently implemented aliases are

Extract and replace elements
bracket `[`
double_bracket `[[`
assign_bracket `[<-`
assign_double_bracket `[[<-`
dollar `$`
at_sign `@`
Arithmetic operators
add `+`
subtract `-`
multiply `*`
divide `/`
integer_divide `%/%`
mod `%%`
raise_to_power `^`
matrix_multiply `%*%`
Logical comparisons
and `&`
or `|`
not `!`
single_and `&&`
single_or `||`
equals `==`
greater_than `>`
greater_or_equal `>=`
less_than `<`
less_or_equal `<=`
is_in `%in%`
Assign attributes
assign_names `names<-`
assign_colnames `colnames<-`
assign_rownames `rownames<-`
assign_dimnames `dimnames<-`
assign_class `class<-`
assign_attributes `attributes<-`
assign_attr `attr<-`
assign_levels `levels<-`
assign_contrasts `contrasts<-`
assign_units `units<-`
assign_comment `comment<-`
assign_diag `diag<-`
assign_dim `dim<-`
assign_length `length<-`
assign_as_na `is.na<-`

Note

Inspired and some alias names adapted from from magrittr. Reused code Copyright (c) 2023 magrittr authors.

Examples

mtcars |>
   bracket(, 1:4)

 1:10 |>
   add(5) |>
   matrix(dimnames = list(letters[1:10], "x")) |>
   matrix_multiply(seq(10, 100, by = 10))

data.frame(1:10, letters[1:10]) |>
  assign_names(c("numbers", "letters"))