Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created December 26, 2013 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ramnathv/8128118 to your computer and use it in GitHub Desktop.
Save ramnathv/8128118 to your computer and use it in GitHub Desktop.
Interactive Controls and rCharts
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Interactive Controls and rCharts</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="icon" type="image/png" href="favicon.ico">
<style>
body {
padding-top: 60px;
padding-bottom: 40px;
}
</style>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap.no-responsive.no-icons.min.css" rel="stylesheet">
<!-- <link rel="stylesheet" href="/css/bootstrap.min.css"> -->
<link rel="stylesheet"
href="http://netdna.bootstrapcdn.com/font-awesome/2.0/css/font-awesome.css">
<link rel="stylesheet" href="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/bootstrap/css/bootstrap-responsive.min.css">
<link rel="stylesheet" href="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/bootstrap/css/main.css">
<link rel="stylesheet" href="http://slidifylibraries2.googlecode.com/git/inst/libraries/highlighters/prettify/css/twitter-bootstrap.css" />
<script src="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/bootstrap/js/vendor/modernizr-2.6.1-respond-1.1.0.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script>window.jQuery || document.write('<script src="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/bootstrap/js/vendor/jquery-1.8.2.min.js"><\/script>')</script>
</head>
<body>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an outdated browser.
<a href="http://browsehappy.com/">Upgrade your browser today</a> or
<a href="http://www.google.com/chromeframe/?redirect=true">
install Google Chrome Frame
</a> to better experience this site.
</p>
<![endif]-->
<!-- Ref: http://twitter.github.com/bootstrap/examples/hero.html -->
<div class="container">
<p><link href='http://fonts.googleapis.com/css?family=Lora|Lato' rel='stylesheet' type='text/css'></p>
<p><link rel="icon"
type="image/png"
href="http://rcharts.io/img/slidify_logo_notext.png"></p>
<style>
.container {
width: 1000px;
}
p {
font-family: "Lora";
font-size: 15px;
line-height: 24px;
text-align: justify;
}
h2 {
font-family: "Lato"
}
</style>
<h2>Interactive Controls and rCharts</h2>
<p id='author' class='text-muted'>by <a href='http://twitter.com/ramnath_vaidya'>Ramnath Vaidyanathan</a>, Dec 25, 2013</p>
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style ">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_button_pinterest_pinit"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
<script type="text/javascript">var addthis_config = {"data_track_addressbar":true};</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4fdfcfd4773d48d3"></script>
<!-- AddThis Button END -->
<p><br/></p>
<p>There was a recent blog post by <a href="https://twitter.com/Sheri_G">Sheri Gilley</a> of <a href="http://www.revolutionanalytics.com/">Revolution Analytics</a> on <a href="http://blog.revolutionanalytics.com/2013/12/combining-the-power-of-deployr-rcharts-and-angularjs.html">Combining the Power of DeployR, rCharts, and AngularJS</a>. It is a very cool application that showcases the power of <a href="http://www.r-project.org/">R</a> and <a href="http://www.revolutionanalytics.com/enterprise-deployment">Revolution R Enterprise</a>, in being able to integrate frameworks like <a href="http://rcharts.io">rCharts</a> and <a href="http://angularjs.org">AngularJS</a>. Although I haven&#39;t had the opportunity to play with <a href="http://www.revolutionanalytics.com/enterprise-deployment">DeployR</a>, I believe that along with <a href="http://www.rstudio.com/shiny/">Shiny</a> and <a href="http://opencpu.org">OpenCPU</a>, it presents a bright future for building and deploying interactive R applications on the web.</p>
<p>Now, what some of you may not know is that integration of simple interactive controls using <a href="http://angularjs.org">AngularJS</a> and <a href="http://davidwalsh.name/dat-gui">DatGUI</a> has been available in <a href="http://rcharts.io">rCharts</a> for quite some time now. The nice thing about using a framework like AngularJS is that a lot of the interactivity can be handled on the client side with minimal amount of code, after the chart has been created from R.</p>
<p>Let us start by creating a simple scatterplot of mileage vs weight of cars from the ubiquitous <code>mtcars</code> dataset.</p>
<pre><code class="r">library(rCharts)
n1 &lt;- rPlot(mpg ~ wt, data = mtcars, color = &quot;gear&quot;, type = &quot;point&quot;)
n1
</code></pre>
<iframe src='
chart1.html
' scrolling='no' seamless></iframe>
<style>iframe{ width: 100%; height: 400px;}</style>
<p>Suppose, we want to let the user choose the <code>x</code>, <code>y</code> and <code>color</code> variables interactively. This can be done using the <code>addControls</code> method, which accepts three arguments: (1) the variable to control, (2) it&#39;s current value and (3) the possible set of values to choose from. By default, <code>addControls</code> uses AngularJS to add the controls.</p>
<pre><code class="r">n1$addControls(&quot;x&quot;, value = &quot;wt&quot;, values = names(mtcars))
n1$addControls(&quot;y&quot;, value = &quot;wt&quot;, values = names(mtcars))
n1$addControls(&quot;color&quot;, value = &quot;gear&quot;, values = names(mtcars))
n1
</code></pre>
<iframe src='
chart2.html
' scrolling='no' seamless></iframe>
<style>iframe{ width: 100%; height: 400px;}</style>
<p>See how a few lines of R code can result in a nice visualization with interactive controls. Cool, right!</p>
<p>Let us build another chart with interactive controls, this time using the <a href="http://nvd3.org">NVD3</a> library for the chart and the <a href="http://davidwalsh.name/dat-gui">DatGUI</a> javascript library for interactive controls. As before, the code stays almost the same, except that we swap the controls template to use <code>datgui</code> instead of <code>angularjs</code>.</p>
<pre><code class="r">HairEye &lt;- subset(as.data.frame(HairEyeColor), Sex == &quot;Male&quot;)
n1 &lt;- nPlot(Freq ~ Eye, data = HairEye, group = &#39;Hair&#39;, type = &#39;multiBarChart&#39;)
n1$addControls(&quot;type&quot;, &quot;multiBarChart&quot;,
values = c(&#39;multiBarChart&#39;, &#39;multiBarHorizontalChart&#39;)
)
n1$setTemplate(script = system.file(&#39;libraries&#39;, &#39;nvd3&#39;, &#39;controls&#39;, &#39;datgui.html&#39;, package = &#39;rCharts&#39;))
n1$set(width = 650)
n1
</code></pre>
<iframe src='
chart3.html
' scrolling='no' seamless></iframe>
<style>iframe{ width: 100%; height: 400px;}</style>
<p>Currently, rCharts only supports simple controls, and my plan is to extend this support. In my next post, I will discuss how rCharts can integrate with a server side framework like <a href="http://rstudio.com/shiny">Shiny</a> or <a href="http://opencpu.org">OpenCPU</a> to allow for greater level of interactivity, that might involve more extensive manipulation of data.</p>
<p><a href="https://gist.github.com/ramnathv/"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a></p>
</div>
</body>
<script src="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/bootstrap/js/vendor/bootstrap.min.js"></script>
<script src="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/bootstrap/js/plugins.js"></script>
<script src="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/bootstrap/js/main.js"></script>
<!-- Load Javascripts for Widgets -->
<!-- Google Prettify -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/prettify/188.0.0/prettify.js"></script>
<script src='http://slidifylibraries2.googlecode.com/git/inst/libraries/highlighters/prettify/js/lang-r.js'></script>
<script>
var pres = document.getElementsByTagName("pre");
for (var i=0; i < pres.length; ++i) {
pres[i].className = "prettyprint linenums";
}
prettyPrint();
</script>
<!-- End Google Prettify -->
</html>
title framework hitheme highlighter mode
Interactive Controls and rCharts
bootstrap
twitter-bootstrap
prettify
standalone
<style> .container { width: 1000px; } p { font-family: "Lora"; font-size: 15px; line-height: 24px; text-align: justify; } h2 { font-family: "Lato" } </style>

Interactive Controls and rCharts

by Ramnath Vaidyanathan, Dec 25, 2013

<script type="text/javascript">var addthis_config = {"data_track_addressbar":true};</script> <script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4fdfcfd4773d48d3"></script>

There was a recent blog post by Sheri Gilley of Revolution Analytics on Combining the Power of DeployR, rCharts, and AngularJS. It is a very cool application that showcases the power of R and Revolution R Enterprise, in being able to integrate frameworks like rCharts and AngularJS. Although I haven't had the opportunity to play with DeployR, I believe that along with Shiny and OpenCPU, it presents a bright future for building and deploying interactive R applications on the web.

Now, what some of you may not know is that integration of simple interactive controls using AngularJS and DatGUI has been available in rCharts for quite some time now. The nice thing about using a framework like AngularJS is that a lot of the interactivity can be handled on the client side with minimal amount of code, after the chart has been created from R.

Let us start by creating a simple scatterplot of mileage vs weight of cars from the ubiquitous mtcars dataset.

library(rCharts)
n1 <- rPlot(mpg ~ wt, data = mtcars, color = "gear", type = "point")
n1
<iframe src=' chart1.html ' scrolling='no' seamless></iframe> <style>iframe{ width: 100%; height: 400px;}</style>

Suppose, we want to let the user choose the x, y and color variables interactively. This can be done using the addControls method, which accepts three arguments: (1) the variable to control, (2) it's current value and (3) the possible set of values to choose from. By default, addControls uses AngularJS to add the controls.

n1$addControls("x", value = "wt", values = names(mtcars))
n1$addControls("y", value = "wt", values = names(mtcars))
n1$addControls("color", value = "gear", values = names(mtcars))
n1
<iframe src=' chart2.html ' scrolling='no' seamless></iframe> <style>iframe{ width: 100%; height: 400px;}</style>

See how a few lines of R code can result in a nice visualization with interactive controls. Cool, right!

Let us build another chart with interactive controls, this time using the NVD3 library for the chart and the DatGUI javascript library for interactive controls. As before, the code stays almost the same, except that we swap the controls template to use datgui instead of angularjs.

HairEye <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Eye, data = HairEye, group = 'Hair', type = 'multiBarChart')
n1$addControls("type", "multiBarChart", 
  values = c('multiBarChart', 'multiBarHorizontalChart')
)
n1$setTemplate(script = system.file('libraries', 'nvd3', 'controls', 'datgui.html', package = 'rCharts'))
n1$set(width = 650)
n1
<iframe src=' chart3.html ' scrolling='no' seamless></iframe> <style>iframe{ width: 100%; height: 400px;}</style>

Currently, rCharts only supports simple controls, and my plan is to extend this support. In my next post, I will discuss how rCharts can integrate with a server side framework like Shiny or OpenCPU to allow for greater level of interactivity, that might involve more extensive manipulation of data.

Fork me on GitHub

---
title: Interactive Controls and rCharts
framework: bootstrap
hitheme: twitter-bootstrap
highlighter: prettify
mode: standalone
---
<link href='http://fonts.googleapis.com/css?family=Lora|Lato' rel='stylesheet' type='text/css'>
<link rel="icon"
type="image/png"
href="http://rcharts.io/img/slidify_logo_notext.png">
<style>
.container {
width: 1000px;
}
p {
font-family: "Lora";
font-size: 15px;
line-height: 24px;
text-align: justify;
}
h2 {
font-family: "Lato"
}
</style>
## Interactive Controls and rCharts
<p id='author' class='text-muted'>by <a href='http://twitter.com/ramnath_vaidya'>Ramnath Vaidyanathan</a>, Dec 25, 2013</p>
<!-- AddThis Button BEGIN -->
<div class="addthis_toolbox addthis_default_style ">
<a class="addthis_button_facebook_like" fb:like:layout="button_count"></a>
<a class="addthis_button_tweet"></a>
<a class="addthis_button_pinterest_pinit"></a>
<a class="addthis_counter addthis_pill_style"></a>
</div>
<script type="text/javascript">var addthis_config = {"data_track_addressbar":true};</script>
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-4fdfcfd4773d48d3"></script>
<!-- AddThis Button END -->
<br/>
There was a recent blog post by [Sheri Gilley](https://twitter.com/Sheri_G) of [Revolution Analytics](http://www.revolutionanalytics.com/) on [Combining the Power of DeployR, rCharts, and AngularJS](http://blog.revolutionanalytics.com/2013/12/combining-the-power-of-deployr-rcharts-and-angularjs.html). It is a very cool application that showcases the power of [R](http://www.r-project.org/) and [Revolution R Enterprise](http://www.revolutionanalytics.com/enterprise-deployment), in being able to integrate frameworks like [rCharts](http://rcharts.io) and [AngularJS](http://angularjs.org). Although I haven't had the opportunity to play with [DeployR](http://www.revolutionanalytics.com/enterprise-deployment), I believe that along with [Shiny](http://www.rstudio.com/shiny/) and [OpenCPU](http://opencpu.org), it presents a bright future for building and deploying interactive R applications on the web.
Now, what some of you may not know is that integration of simple interactive controls using [AngularJS](http://angularjs.org) and [DatGUI](http://davidwalsh.name/dat-gui) has been available in [rCharts](http://rcharts.io) for quite some time now. The nice thing about using a framework like AngularJS is that a lot of the interactivity can be handled on the client side with minimal amount of code, after the chart has been created from R.
Let us start by creating a simple scatterplot of mileage vs weight of cars from the ubiquitous `mtcars` dataset.
```{r setup, echo = F, results = 'asis', cache = F}
library(knitr)
opts_chunk$set(tidy = F, comment = NA, results = 'asis', fig.path = "")
options(rcharts.mode = 'iframe', rcharts.cdn = TRUE)
```
```{r chart1}
library(rCharts)
n1 <- rPlot(mpg ~ wt, data = mtcars, color = "gear", type = "point")
n1
```
Suppose, we want to let the user choose the `x`, `y` and `color` variables interactively. This can be done using the `addControls` method, which accepts three arguments: (1) the variable to control, (2) it's current value and (3) the possible set of values to choose from. By default, `addControls` uses AngularJS to add the controls.
```{r chart2}
n1$addControls("x", value = "wt", values = names(mtcars))
n1$addControls("y", value = "wt", values = names(mtcars))
n1$addControls("color", value = "gear", values = names(mtcars))
n1
```
Let us build another chart with interactive controls, this time using the [NVD3](http://nvd3.org) library for the chart and the [DatGUI](http://davidwalsh.name/dat-gui) javascript library for interactive controls. As before, the code stays almost the same, except that we swap the controls template to use `datgui` instead of `angularjs`.
```{r chart3}
HairEye <- subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Eye, data = HairEye, group = 'Hair', type = 'multiBarChart')
n1$addControls("type", "multiBarChart",
values = c('multiBarChart', 'multiBarHorizontalChart')
)
n1$setTemplate(script = system.file('libraries', 'nvd3', 'controls', 'datgui.html', package = 'rCharts'))
n1$set(width = 650)
n1
```
Currently, rCharts only supports simple controls, and my plan is to extend this support. In my next post, I will discuss how rCharts can integrate with a server side framework like [Shiny](http://rstudio.com/shiny) or [OpenCPU](http://opencpu.org) to allow for greater level of interactivity, that might involve more extensive manipulation of data.
<a href="https://gist.github.com/ramnathv/"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment