Compiling CoffeeScript in R with the js package

February 26, 2015

[This article was first published on OpenCPU, 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.

opencpu logo

A new release of the js package has made it’s way to CRAN. This version adds support for compiling Coffee Script. Along with the uglify and jshint tools already in there, the package now provides a very complete suite for compiling, validating, reformating, optimizing and analyzing JavaScript code in R.

Coffee Script

According to its website, CoffeeScript is a little language that compiles into JavaScript. It is an attempt to expose the good parts of JavaScript in a simple way. The coffee_compile function binds to the coffee script compiler. A hello world example from the package vignette:

# Hello world
cat(coffee_compile("square = (x) -> x * x"))

This outputs the following JavaScript code:

(function() {
  var square;

  square = function(x) {
    return x * x;


Or to compile without the closure:

# Hello world
cat(coffee_compile("square = (x) -> x * x", bare = TRUE))
var square;

square = function(x) {
  return x * x;

The package vignette includes some more examples.

Why coffee script?

Coffee script is not some sort of widget factory or other “use JavaScript without learning JavaScript” tool kit. From the website:

The golden rule of CoffeeScript is: “It’s just JavaScript”. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript.

CoffeeScript is popular among web developers for writing JavaScript applications using a syntax that is more readable and less error prone, but without being constrained by some sort of framework. CoffeeScript is often used in conjunction with an HTML templating engine such as jade (see rjade) and a CSS pre-processor such as Less or SASS or Stylus.

Together, these tools are helpful in organizing and maintaining a non-trivial web applications. Given the recent mass adoption of HTML/JavaScipt based widgets and vizualisation in the R community, they can be a valuable addition to the R developer tool kit as well.

To leave a comment for the author, please follow the link and comment on their blog: OpenCPU. 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.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)