Interactive JS Charts from R

require(devtools)
install_github('rCharts', 'ramnathv')

View the Project on GitHub ramnathv/rCharts

rCharts | One Interface, Multiple JS Libraries

Recently, I had blogged about two R packages, rCharts and rNVD3 that provided R users a lattice like interface to create interactive visualizations using popular javascript libraries.

There was a lot of repeated code between the two packages, which lead me to think that it might be possible to integrates multiple JS libraries into a single package with a common lattice like interface. After heavy refactoring, I finally managed to implement three popular JS libraries in rCharts: Polycharts, NVD3 and MorrisJS.

rCharts uses reference classes, which I believe is one of the best things to happen to R. It allowed me to keep the code base pretty concise, while implementing a fair degree of functionality. The current structure of rCharts should make it easy to integrate any JS visualization library that uses a configuration variable to create charts. This includes Highcharts and Vega, which have R implementations in rHighcharts and rVega.

A huge advantage of wrapping these libraries within the same package is that they can take advantage of the common code. For example, when I implemented MorrisJS, I only had to focus on the data structure required by the library. All the other parts like rendering, integration with shiny and slidify, were derived from the parent class.

All JS libraris in rCharts are compatible with Slidify. In fact, this blog post was created by running Slidify on the source Rmd file! I have provided one example for each of the three libraries to give you a flavor of what is possible, and will be posting more examples shortly.

If you have an interesting JS plotting library that you would like to see added, or have feedback, please post here on github.

Example 1: Polycharts

require(rCharts)
names(iris) = gsub("\\.", "", names(iris))
p1 <- rPlot(SepalLength ~ SepalWidth | Species, data = iris, color = 'Species', 
  type = 'point')
p1$set(width = 550)
p1$print('chart1') # use p1$show() from your R console 

Example 2: NVD3

p2 <- nvd3Plot(SepalLength ~ SepalWidth, group = 'Species', data = iris, 
  type = 'scatterChart')
p2$set(width = 550)
p2$print('chart2')

Example 3: MorrisJS

data(economics, package = 'ggplot2')
dat = transform(economics, date = as.character(date))
p3 <- mPlot(x = "date", y = list("psavert", "uempmed"), data = dat, type = 'Area',
  labels = list('Savings Rate', 'Median Duration of Unemployment'), pointSize = 0)
p3$print('chart3')