In some computing subcultures, the term "grapes" refers to the percent sign, which R uses to name binary operators such as %*%
. Here, grapes
is an R package that turns arbitrary functions into binary operators. As with the magrittr pipe, this allows you to avoid cumbersome parentheses in your code.
library(grapes) grow(rbind, c, from = "base") # Use `from` to specify a package or environment to search. bunch() nrow(sleep) longer = sleep %rbind% sleep %rbind% sleep nrow(longer) 1 %rbind% 2 %c% 3
In your workspace, you can define your own functions for your operators. That way, there is no need to set the from
argument to tell grow()
where to look.
myop <- function(x, y){ 1/x + 1/y } grow(myop) 2 %myop% 3
Use bunch()
and functions()
to list the available operators and non-operator functions.
bunch() functions()
Operators are left-associative!
2 %myop% 3 %myop% 4 (2 %myop% 3) %myop% 4 2 %myop% (3 %myop% 4)
You can even turn all available functions from a package or environment into operators. But be warned: there is no guarantee that these operators will actually work.
functions("knitr") bunch("knitr") grow(from = "knitr") bunch() `%purl%`
Advanced users can supply environments to the from
and to
arguments. This affords more control over where the functions come from and where the operators get assigned.
to_env = new.env() from_env = new.env() from_env$nextop = function(a, b, extra = 3){ sqrt(a^2 + b^2) + extra } assign_operator = function(){ grow(nextop, from = from_env, to = to_env) } assign_operator() # 1 %nextop% 2 # throws an error since %nextop% is not defined in your workspace eval(parse(text = "1 %nextop% 2"), envir = to_env) bunch(from_env) functions(from_env) bunch(to_env) functions(to_env)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.