Skip to content

hughjonesd/r-source

 
 

Repository files navigation

A Pipe Assignment Operator for R

This is a build of R that adds an experimental pipe assignment operator <|>.

LHS <|> RHS is parsed as LHS <- RHS(LHS, ...).

Examples

x <|> sqrt()             # becomes  x <- sqrt(x)
x[item] <|> foo()       # becomes x[item] <- foo(x[item])
x$elem <|> foo()        # becomes  x$elem <- foo(x$elem)
names(x) <|> toupper()  # becomes  names(x) <- toupper(names(x))
x <|> gsub("pattern", "replacement", x = _)
                        # becomes  x <- gsub("pattern", "replacement", x = x)

Arbitrarily complex left hand sides are allowed, including assignment functions and subassignment.

Chaining <|> is not allowed (and would not make sense).

Rationale

For a detailed rationale, see https://hughjonesd.github.io/case-for-pipe-assignment.html.

The skinny:

  • R is pass-by-value and allows complex subassignment
  • Often we want to simply pass variables, or parts of them, through a function
  • Doing this involves repeating oneself

In other words, it would be nice to write

names(x)[1:5] <|> toupper()

instead of

names(x)[1:5] <- toupper(names(x)[1:5])

and

my_data[rows, cols] <|> as.numeric()

instead of

my_data[rows, cols] <- as.numeric(my_data[rows, cols])

Building

You can build this version from github source using the build-R.sh script. Alternatively, if you have an existing R source tree, you can apply the provided patch:

cd /path/to/r-source
patch -p1 < pipe-assignment-operator.patch

The patch file pipe-assignment-operator.patch modifies:

  • src/main/gram.y - Grammar file with pipe assignment operator
  • src/main/gram.c - Generated parser (regenerated from gram.y)
  • tests/reg-tests-pipe-assign.R - Test suite for the operator

Afterwards, build R as usual (see the wch/r-source wiki for build instructions from github).

Vibe-coded with Claude

Yeah, this was vibe-coded! Claude basically one-shotted the Yacc, which is much beyond my capability.

About

Read-only mirror of R source code from https://svn.r-project.org/R/, updated hourly. See the build instructions on the wiki page.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 38.0%
  • C 31.6%
  • Fortran 23.8%
  • M4 1.1%
  • Shell 1.1%
  • TeX 1.1%
  • Other 3.3%