# Three Syntax Additions that Would Make R Even More Awesome

So R is awesome. Felt good to get that out of the way! But sometimes I long for some small syntax additions while programming away in R in the night…

The picture is of a restaurant called Risotto in Berlin.

Just to make it clear, I fully understand that changing the syntax of a language as popular as R is a pretty big thing and I’m not arguing that the changes I propose below actually should be implemented. There are probably many good reasons for why it would be a pretty bad idea to implement these syntax changes. It is also very likely that there are reasons for why these syntax changes would not be possible to implement at all. That still doesn’t mean it wouldn’t be cool if these were implemented :)

## Addition 1: Shorthand Function Declaration

I find that I spend a lot of time defining functions in R. When defining standalone functions the current syntax is great, but most functions I define are anonymous functions declared within calls to functions such as apply and ddply. For example:

For such cases it would be nice if there were some shorthand function declaration syntax. It’s not that function takes a particularly long time to write, it’s more that it takes up visual space and distracts from what is actually happening. So what could this shorthand look like? One alternative would simply be:

If we write c(1, 2, 3) instead of combine(1 , 2, 3) why couldn’t we write f(a,b) {a + b} instead of function(a,b) { a + b}.

Another alternative, instpired by Haskel, would be to use a \ as in:

We could even (but this is a bit crazy…) drop the parentheses…

… which would make the above call of ddply look like:

Since many functions are vectorized in R it is often easy to chain together multiple function and doing a lot of work in just one line. This is great but easily results in nested statements which are hard to decipher because you can’t read them from left to right but rather have to read them from the “inside out”. For example, I’ve written statements far worse than this:

But this is still not easy to get directly, to what function call does na.rm=TRUE belong to, for example. As proposed by Robert Sugar on the R-help mailing list, statements like these would be easier to write if there existed a pipe-like operator that took the result of the statement on the left and “piped” it to the function on the right. Let’s arbitrarily use a period . for this operator. Then the following:

… could be written as:

We could add a further rule: If the function on the right of the pipe operator already has arguments filled in we insert the result on the left of the operator as the first argument to the function on the right. Then the following:

… could be written as:

As R is already object oriented a pipe operator would make it possible to write code in a similar left-to-right fashion as in other object oriented languages. Now, the period . would be pretty impossible to use as the pipe operator as it is a legal and common part of many identifiers. I’m not sure what would be a good alternative… perhaps ->? Nobody uses that for assignment anyway, right? :)

## Addition 3: Ruby style yields

The Ruby language has a very useful feature which allows you to pass an anonymous function as an argument by appending the anonymous function to the end of the function call. That is, a Ruby function that takes a function and calls it 10 times like this:

Could be rewritten to be called like this:

This could be implemented in R by the following rule: If an object follows after the closing parenthesis of a function call add it as the last argument of the function call and bind it to, for example, the .yield variable in the called function. Using this rule the following:

… could be written as:

Above we passed an expression but we could also pass a function:

Using all three proposed syntax additions, the following:

… could be rewritten as:

So, this was just some ideas… Don’t take them to seriously :)