prefix: Facilities for writing R code in prefix notation

Description Usage Arguments Details Value Examples

View source: R/prefix.R

Description

The . function allows writing an R function call in a Lisp-like prefix format, rather than the Algol-like infix format that R usually uses. prefix and infix convert R code between infix and prefix.

Usage

1
2
3
4
5
prefix(expr)

infix(expr)

.(f, ...)

Arguments

expr

A prefix- or infix-formatted expression to convert. Code between "R(" and ")" will not be converted.

f

The function that . should generate a call to.

...

Further arguments to the function f.

Details

When evaluated, a call to . doesn't actually execute the function that's been expressed in prefix style, just generates the corresponding infix call. It's intended as a notational aid, so that entire scripts can be written in the prefix form.

A call to prefix() does not evaluate the passed or the generated expressions, partly for efficiency reasons; infix(), though it still does not evaluate the generated expression, does do partial evaluation of the passed expression: each call to . is evaluated to the corresponding infix call.

infix and prefix are not quite inverses of each other, because infix understands certain pieces of infix syntactic sugar. Examples include the ".q" and ".b" quoting abbreviations, and the lambda lists passed to lambda, macro and defmacro.

Value

prefix and infix return the input expression converted to prefix or infix.

. returns an unevaluated call to f with ... arguments carried through.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
prefix(expression(x <- runif(10), y <- runif(10), cor(x, y)))

prefix(expression(with(datasets::iris, sum(Sepal.Length^2 + Petal.Width))))


infix(expression(.(`<-`, x, .(runif, 10)), .(`<-`, y, .(runif, 10)),
                 .(cor, x, y)))

infix(expression(.(with, .(`::`, datasets, iris),
                             .(sum, .(`+`, .(`^`, Sepal.Length, 2),
                                           Petal.Width)))))
.(`+`, 1, 2)

.(sum, .(runif, 10), .(runif, 10))

wwbrannon/schemeR documentation built on May 4, 2019, 12:03 p.m.