Site icon R-bloggers

New syntax proposed for R language

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

R user and developer Lionel Henry proposes a number of changes to R syntax:

Use square brackets to create lists. You could use [1, 2:5, "hello"] to create a list of three elements. Nested lists would be possible as well, with syntax like or [ [1, 2], [2, 3] ] (much easier than list(list(1,2),list(2,3))).

Define lambda functions with square brackets. You could create an unnamed function like [x] -> log(abs(x)) to transform x to the log of its positive part. This is a little easier than using calling function(x) log(abs(x)) .

Allow labeled blocks. This change would make it easier to pass blocks of code into functions. You could write:
test_that("my code works") {
  check_equal(A,B)
  check_identical(C, D)
}

instead of
test_that("my code works", {
 check_equal(A,B)
 check_identical(C, D)
})
It would also allow you to create your own functions that have similar syntax (like a custom if statement).

A native piping operator. Magrittr's %>% pipe operator would get a new look, so that mtcars |> unlist would be a standard part of the language. This would also make it easier to debug code that used the pipe.

R is a very flexible language that provides many native facilities to extend the language itself. The %>% operator is defined as a "special operator" in the magrittr package, and you can even define Haskell-like list comprehensions for R by redefining the [ operator yourself. That approach won't work Lionel's changes above, though: because they modify R syntax they need to be made at the R source code level. (You can find Lionel's changes in this fork of the R source tree on GitHub.) That means it's fairly unlikely they'll be incorporated by R Core (not least because of the potential for backwards compatibility issues) but you never know.

You can read Lionel's full description of his proposed changes at the blog post linked below. His post doesn't have a comments section, so let us know what you think of the changes in the comments here.

(routines …): The future of R syntax?

To leave a comment for the author, please follow the link and comment on their blog: Revolutions.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.