Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bwiernik/pipebind

Flexible binding for complex function evaluation with the Base R |> pipe
https://github.com/bwiernik/pipebind

Last synced: about 2 months ago
JSON representation

Flexible binding for complex function evaluation with the Base R |> pipe

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# 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:

```r
install.packages("pipebind")
```

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

``` r
remotes::install_github("bwiernik/pipebind")
```

## Examples

```{r example}
library(pipebind)

set.seed(2016)

# 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) |>
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)
```

## Code of Conduct

Please note that the pipebind project is released with a [Contributor Code of Conduct](https://contributor-covenant.org/version/2/0/CODE_OF_CONDUCT.html). By contributing to this project, you agree to abide by its terms.