Compiling CoffeeScript in R with the js package

February 26, 2015
By

(This article was first published on OpenCPU, and kindly contributed to R-bloggers)

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;
  };

}).call(this);

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.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



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.

Sponsors

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)