MyNixOS website logo
Description

Flexible Binding for Complex Function Evaluation with the Base R |>.

Provides a simple function to bind a piped object to a placeholder symbol to enable complex function evaluation with the base R |> pipe.

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

The base R |> pipe lacks some advanced functionality compared to the {magrittr}%>% pipe. Most notably, 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) and cannot be used with some in-line functions (e.g., +).

The |> pipe had additional limitations in earlier versions of R (e.g., the _ placeholder was not available before R 4.2.0; the _ placeholder could not appear on the left side of sub-setting functions like $, [, [[, or @ before R 4.3.0).

This package provides a bind() function as 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 package also provides aliases for in-line functions like + and %in% to facilitate their use with the |> pipe.

Installation

You can install {pipebind} from CRAN:

install.packages("pipebind")

You can install the development version of {pipebind} like so:

remotes::install_github("bwiernik/pipebind")

Examples

library(pipebind)

set.seed(2016)

# Piping to a non-first argument
mtcars |>
  transform(kmL = mpg / 2.35) |>
  bind(d, lm(kmL ~ hp, data = d))
#> 
#> Call:
#> lm(formula = kmL ~ hp, data = d)
#> 
#> Coefficients:
#> (Intercept)           hp  
#>    12.80803     -0.02903

# Using the piped value multiple times
rnorm(10) |>
  bind(x, x - mean(x))
#>  [1] -0.55014875  1.36584095  0.30817018  0.66123825 -2.42687776  0.08185269
#>  [7] -0.39891573 -0.32041684  0.73166705  0.54758995

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

# Subsetting the piped value
mtcars |>
  bind(d, d$mpg)
#>  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
#> [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
#> [31] 15.0 21.4

Code of Conduct

Please note that the pipebind project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Metadata

Version

0.1.2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows