Skip to content

Instantly share code, notes, and snippets.

@ramnathv
Created December 22, 2013 15:41
Show Gist options
  • Save ramnathv/8084330 to your computer and use it in GitHub Desktop.
Save ramnathv/8084330 to your computer and use it in GitHub Desktop.
Embedding rCharts
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Embedding rCharts</title>
<link rel="stylesheet" href="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/minimal/stylesheets/styles.css">
<link rel="stylesheet" href="http://slidifylibraries2.googlecode.com/git/inst/libraries/highlighters/prettify/css/twitter-bootstrap.css">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="wrapper">
<header>
<h1>Embedding rCharts</h1>
<p></p>
<ol>
</ol>
</header>
<section>
<p><link href='http://fonts.googleapis.com/css?family=Lora' rel='stylesheet' type='text/css'></p>
<style>
p {font-family: "Lora"; text-align: justify;}
ol.linenums{margin-left: -5px;}
</style>
<h2>Embedding rCharts in R Markdown</h2>
<p>by <a href="http://github.com/ramnathv">Ramnath Vaidyanathan</a>, Dec 22, 2003</p>
<p>This is a short note to address a question from <a href="@sharon000">Sharon Machlis</a> on the best way to embed a visualization created using <a href="http://rcharts.io">rCharts</a>, so that one can create a standalone HTML file using <code>knit2html</code>. If you have not used rCharts, you can try it out using the <a href="http://rcharts.io/playground">online playground</a></p>
<blockquote class="twitter-tweet" lang="en"><p><a href="https://twitter.com/ramnath_vaidya">@ramnath_vaidya</a> Is this still best practice for knit2html to create rCharts file for use off your local machine? TNX! <a href="http://t.co/10RfiH9XgG">http://t.co/10RfiH9XgG</a></p>&mdash; Sharon Machlis (@sharon000) <a href="https://twitter.com/sharon000/statuses/414743096279441408">December 22, 2013</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
<p>You will need the latest version of <code>rCharts</code> (&gt; 0.4) to use the approaches suggested in this note.</p>
<pre><code class="r">library(devtools)
install_github(&quot;rCharts&quot;, &quot;ramnathv&quot;)
</code></pre>
<h2>Create Chart</h2>
<p>Let us first create a simple barplot using NVD3</p>
<pre><code class="r">library(rCharts)
hair_eye_male = subset(as.data.frame(HairEyeColor), Sex == &quot;Male&quot;)
n1 &lt;- nPlot(Freq ~ Hair, group = &#39;Eye&#39;,
data = hair_eye_male, type = &#39;multiBarChart&#39;
)
n1$set(width = 600)
</code></pre>
<p>We need to set the chunk options <code>comment = NA</code> and <code>results = &quot;asis&quot;</code> so that the resulting html is rendered asis and not marked up (which is the default in knitr). Make sure to set <code>cache = F</code> for this chunk so that it is always run.</p>
<pre><code class="r">library(knitr)
opts_chunk$set(comment = NA, results = &quot;asis&quot;, comment = NA, tidy = F)
</code></pre>
<h2>Embed Option 1: IFrame Inline</h2>
<p>The first option is to embed the chart as an inline iframe. It has the advantage of keeping the html standalone, but isolating the chart from the html on the page, thereby avoiding css and js conflicts. However, this feature is <a href="http://www.w3schools.com/tags/att_iframe_srcdoc.asp">not supported by IE and Opera</a>.</p>
<pre><code class="r">n1$show(&#39;iframesrc&#39;, cdn = TRUE)
</code></pre>
<iframe srcdoc='
&lt;!doctype HTML&gt;
&lt;meta charset = &#039;utf-8&#039;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;link rel=&#039;stylesheet&#039; href=&#039;http://nvd3.org/src/nv.d3.css&#039;&gt;
&lt;script src=&#039;http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js&#039; type=&#039;text/javascript&#039;&gt;&lt;/script&gt;
&lt;script src=&#039;http://d3js.org/d3.v3.min.js&#039; type=&#039;text/javascript&#039;&gt;&lt;/script&gt;
&lt;script src=&#039;http://timelyportfolio.github.io/rCharts_nvd3_tests/libraries/widgets/nvd3/js/nv.d3.min-new.js&#039; type=&#039;text/javascript&#039;&gt;&lt;/script&gt;
&lt;script src=&#039;http://nvd3.org/lib/fisheye.js&#039; type=&#039;text/javascript&#039;&gt;&lt;/script&gt;
&lt;style&gt;
.rChart {
display: block;
margin-left: auto;
margin-right: auto;
width: 600px;
height: 400px;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&#039;chart13be6cad89a5&#039; class=&#039;rChart nvd3&#039;&gt;&lt;/div&gt;
&lt;script type=&#039;text/javascript&#039;&gt;
$(document).ready(function(){
drawchart13be6cad89a5()
});
function drawchart13be6cad89a5(){
var opts = {
&quot;dom&quot;: &quot;chart13be6cad89a5&quot;,
&quot;width&quot;: 600,
&quot;height&quot;: 400,
&quot;x&quot;: &quot;Hair&quot;,
&quot;y&quot;: &quot;Freq&quot;,
&quot;group&quot;: &quot;Eye&quot;,
&quot;type&quot;: &quot;multiBarChart&quot;,
&quot;id&quot;: &quot;chart13be6cad89a5&quot;
},
data = [
{
&quot;Hair&quot;: &quot;Black&quot;,
&quot;Eye&quot;: &quot;Brown&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 32
},
{
&quot;Hair&quot;: &quot;Brown&quot;,
&quot;Eye&quot;: &quot;Brown&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 53
},
{
&quot;Hair&quot;: &quot;Red&quot;,
&quot;Eye&quot;: &quot;Brown&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 10
},
{
&quot;Hair&quot;: &quot;Blond&quot;,
&quot;Eye&quot;: &quot;Brown&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 3
},
{
&quot;Hair&quot;: &quot;Black&quot;,
&quot;Eye&quot;: &quot;Blue&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 11
},
{
&quot;Hair&quot;: &quot;Brown&quot;,
&quot;Eye&quot;: &quot;Blue&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 50
},
{
&quot;Hair&quot;: &quot;Red&quot;,
&quot;Eye&quot;: &quot;Blue&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 10
},
{
&quot;Hair&quot;: &quot;Blond&quot;,
&quot;Eye&quot;: &quot;Blue&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 30
},
{
&quot;Hair&quot;: &quot;Black&quot;,
&quot;Eye&quot;: &quot;Hazel&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 10
},
{
&quot;Hair&quot;: &quot;Brown&quot;,
&quot;Eye&quot;: &quot;Hazel&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 25
},
{
&quot;Hair&quot;: &quot;Red&quot;,
&quot;Eye&quot;: &quot;Hazel&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 7
},
{
&quot;Hair&quot;: &quot;Blond&quot;,
&quot;Eye&quot;: &quot;Hazel&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 5
},
{
&quot;Hair&quot;: &quot;Black&quot;,
&quot;Eye&quot;: &quot;Green&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 3
},
{
&quot;Hair&quot;: &quot;Brown&quot;,
&quot;Eye&quot;: &quot;Green&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 15
},
{
&quot;Hair&quot;: &quot;Red&quot;,
&quot;Eye&quot;: &quot;Green&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 7
},
{
&quot;Hair&quot;: &quot;Blond&quot;,
&quot;Eye&quot;: &quot;Green&quot;,
&quot;Sex&quot;: &quot;Male&quot;,
&quot;Freq&quot;: 8
}
]
if(!(opts.type===&quot;pieChart&quot; || opts.type===&quot;sparklinePlus&quot;)) {
var data = d3.nest()
.key(function(d){
//return opts.group === undefined ? &#039;main&#039; : d[opts.group]
//instead of main would think a better default is opts.x
return opts.group === undefined ? opts.y : d[opts.group];
})
.entries(data);
}
if (opts.disabled != undefined){
data.map(function(d, i){
d.disabled = opts.disabled[i]
})
}
nv.addGraph(function() {
var chart = nv.models[opts.type]()
.x(function(d) { return d[opts.x] })
.y(function(d) { return d[opts.y] })
.width(opts.width)
.height(opts.height)
d3.select(&quot;#&quot; + opts.id)
.append(&#039;svg&#039;)
.datum(data)
.transition().duration(500)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
};
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
' scrolling='no' seamless></iframe>
<style>iframe{ width: 100%; height: 400px;}</style>
<h2>Embed Option 2: Inline</h2>
<p>This option embeds the chart inline in the html. It should work in all browsers that the charting library being used supports. However, it is susceptible to css and js conflicts.</p>
<pre><code class="r">n1$show(&#39;inline&#39;, include_assets = TRUE, cdn = TRUE)
</code></pre>
<p><link rel='stylesheet' href=http://nvd3.org/src/nv.d3.css></p>
<script type='text/javascript' src=http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js></script>
<script type='text/javascript' src=http://d3js.org/d3.v3.min.js></script>
<script type='text/javascript' src=http://timelyportfolio.github.io/rCharts_nvd3_tests/libraries/widgets/nvd3/js/nv.d3.min-new.js></script>
<script type='text/javascript' src=http://nvd3.org/lib/fisheye.js></script>
<p><style>
.rChart {
display: block;
margin-left: auto;
margin-right: auto;
width: 600px;
height: 400px;
}<br>
</style></p>
<div id = 'chart13be6261be152' class = 'rChart nvd3'></div>
<script type='text/javascript'>
$(document).ready(function(){
drawchart13be6261be152()
});
function drawchart13be6261be152(){
var opts = {
"dom": "chart13be6261be152",
"width": 600,
"height": 400,
"x": "Hair",
"y": "Freq",
"group": "Eye",
"type": "multiBarChart",
"id": "chart13be6261be152"
},
data = [
{
"Hair": "Black",
"Eye": "Brown",
"Sex": "Male",
"Freq": 32
},
{
"Hair": "Brown",
"Eye": "Brown",
"Sex": "Male",
"Freq": 53
},
{
"Hair": "Red",
"Eye": "Brown",
"Sex": "Male",
"Freq": 10
},
{
"Hair": "Blond",
"Eye": "Brown",
"Sex": "Male",
"Freq": 3
},
{
"Hair": "Black",
"Eye": "Blue",
"Sex": "Male",
"Freq": 11
},
{
"Hair": "Brown",
"Eye": "Blue",
"Sex": "Male",
"Freq": 50
},
{
"Hair": "Red",
"Eye": "Blue",
"Sex": "Male",
"Freq": 10
},
{
"Hair": "Blond",
"Eye": "Blue",
"Sex": "Male",
"Freq": 30
},
{
"Hair": "Black",
"Eye": "Hazel",
"Sex": "Male",
"Freq": 10
},
{
"Hair": "Brown",
"Eye": "Hazel",
"Sex": "Male",
"Freq": 25
},
{
"Hair": "Red",
"Eye": "Hazel",
"Sex": "Male",
"Freq": 7
},
{
"Hair": "Blond",
"Eye": "Hazel",
"Sex": "Male",
"Freq": 5
},
{
"Hair": "Black",
"Eye": "Green",
"Sex": "Male",
"Freq": 3
},
{
"Hair": "Brown",
"Eye": "Green",
"Sex": "Male",
"Freq": 15
},
{
"Hair": "Red",
"Eye": "Green",
"Sex": "Male",
"Freq": 7
},
{
"Hair": "Blond",
"Eye": "Green",
"Sex": "Male",
"Freq": 8
}
]
if(!(opts.type==="pieChart" || opts.type==="sparklinePlus")) {
var data = d3.nest()
.key(function(d){
//return opts.group === undefined ? 'main' : d[opts.group]
//instead of main would think a better default is opts.x
return opts.group === undefined ? opts.y : d[opts.group];
})
.entries(data);
}
if (opts.disabled != undefined){
data.map(function(d, i){
d.disabled = opts.disabled[i]
})
}
nv.addGraph(function() {
var chart = nv.models[opts.type]()
.x(function(d) { return d[opts.x] })
.y(function(d) { return d[opts.y] })
.width(opts.width)
.height(opts.height)
d3.select("#" + opts.id)
.append('svg')
.datum(data)
.transition().duration(500)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
};
</script>
</section>
<footer>
<p>Maintained by <a href="http://github.com/">
Ramnath Vaidyanathan
</a></p>
<p><small>Hosted on GitHub Pages &mdash; Theme by
<a href="https://github.com/orderedlist">orderedlist</a></small>
</p>
</footer> </div>
<script src="http://slidifylibraries2.googlecode.com/git/inst/libraries/frameworks/minimal/javascripts/scale.fix.js"></script>
</body>
<!-- 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 author framework highlighter hitheme mode
Embedding rCharts
Ramnath Vaidyanathan
minimal
prettify
twitter-bootstrap
standalone
<style> p {font-family: "Lora"; text-align: justify;} ol.linenums{margin-left: -5px;} </style>

Embedding rCharts in R Markdown

by Ramnath Vaidyanathan, Dec 22, 2003

This is a short note to address a question from Sharon Machlis on the best way to embed a visualization created using rCharts, so that one can create a standalone HTML file using knit2html. If you have not used rCharts, you can try it out using the online playground

@ramnath_vaidya Is this still best practice for knit2html to create rCharts file for use off your local machine? TNX! http://t.co/10RfiH9XgG

— Sharon Machlis (@sharon000) December 22, 2013
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>

You will need the latest version of rCharts (> 0.4) to use the approaches suggested in this note.

library(devtools)
install_github("rCharts", "ramnathv")

Create Chart

Let us first create a simple barplot using NVD3

library(rCharts)
hair_eye_male = subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = 'Eye', 
  data = hair_eye_male, type = 'multiBarChart'
)
n1$set(width = 600)

We need to set the chunk options comment = NA and results = "asis" so that the resulting html is rendered asis and not marked up (which is the default in knitr). Make sure to set cache = F for this chunk so that it is always run.

library(knitr)
opts_chunk$set(comment = NA, results = "asis", comment = NA, tidy = F)

Embed Option 1: IFrame Inline

The first option is to embed the chart as an inline iframe. It has the advantage of keeping the html standalone, but isolating the chart from the html on the page, thereby avoiding css and js conflicts. However, this feature is not supported by IE and Opera.

n1$show('iframesrc', cdn = TRUE)
<iframe srcdoc=' <!doctype HTML> <meta charset = 'utf-8'> <html> <head> <link rel='stylesheet' href='http://nvd3.org/src/nv.d3.css'>
&lt;script src=&#039;http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js&#039; type=&#039;text/javascript&#039;&gt;&lt;/script&gt;
&lt;script src=&#039;http://d3js.org/d3.v3.min.js&#039; type=&#039;text/javascript&#039;&gt;&lt;/script&gt;
&lt;script src=&#039;http://timelyportfolio.github.io/rCharts_nvd3_tests/libraries/widgets/nvd3/js/nv.d3.min-new.js&#039; type=&#039;text/javascript&#039;&gt;&lt;/script&gt;
&lt;script src=&#039;http://nvd3.org/lib/fisheye.js&#039; type=&#039;text/javascript&#039;&gt;&lt;/script&gt;

&lt;style&gt;
.rChart {
  display: block;
  margin-left: auto; 
  margin-right: auto;
  width: 600px;
  height: 400px;
}  
&lt;/style&gt;

</head> <body> <div id='chart13be6cad89a5' class='rChart nvd3'></div>

&lt;script type=&#039;text/javascript&#039;&gt;

$(document).ready(function(){ drawchart13be6cad89a5() }); function drawchart13be6cad89a5(){
var opts = { "dom": "chart13be6cad89a5", "width": 600, "height": 400, "x": "Hair", "y": "Freq", "group": "Eye", "type": "multiBarChart", "id": "chart13be6cad89a5" }, data = [ { "Hair": "Black", "Eye": "Brown", "Sex": "Male", "Freq": 32 }, { "Hair": "Brown", "Eye": "Brown", "Sex": "Male", "Freq": 53 }, { "Hair": "Red", "Eye": "Brown", "Sex": "Male", "Freq": 10 }, { "Hair": "Blond", "Eye": "Brown", "Sex": "Male", "Freq": 3 }, { "Hair": "Black", "Eye": "Blue", "Sex": "Male", "Freq": 11 }, { "Hair": "Brown", "Eye": "Blue", "Sex": "Male", "Freq": 50 }, { "Hair": "Red", "Eye": "Blue", "Sex": "Male", "Freq": 10 }, { "Hair": "Blond", "Eye": "Blue", "Sex": "Male", "Freq": 30 }, { "Hair": "Black", "Eye": "Hazel", "Sex": "Male", "Freq": 10 }, { "Hair": "Brown", "Eye": "Hazel", "Sex": "Male", "Freq": 25 }, { "Hair": "Red", "Eye": "Hazel", "Sex": "Male", "Freq": 7 }, { "Hair": "Blond", "Eye": "Hazel", "Sex": "Male", "Freq": 5 }, { "Hair": "Black", "Eye": "Green", "Sex": "Male", "Freq": 3 }, { "Hair": "Brown", "Eye": "Green", "Sex": "Male", "Freq": 15 }, { "Hair": "Red", "Eye": "Green", "Sex": "Male", "Freq": 7 }, { "Hair": "Blond", "Eye": "Green", "Sex": "Male", "Freq": 8 } ]

  if(!(opts.type===&quot;pieChart&quot; || opts.type===&quot;sparklinePlus&quot;)) {
    var data = d3.nest()
      .key(function(d){
        //return opts.group === undefined ? &#039;main&#039; : d[opts.group]
        //instead of main would think a better default is opts.x
        return opts.group === undefined ? opts.y : d[opts.group];
      })
      .entries(data);
  }
  
  if (opts.disabled != undefined){
    data.map(function(d, i){
      d.disabled = opts.disabled[i]
    })
  }
  
  nv.addGraph(function() {
    var chart = nv.models[opts.type]()
      .x(function(d) { return d[opts.x] })
      .y(function(d) { return d[opts.y] })
      .width(opts.width)
      .height(opts.height)
     
    
      
    

    
    
    
  
   d3.select(&quot;#&quot; + opts.id)
    .append(&#039;svg&#039;)
    .datum(data)
    .transition().duration(500)
    .call(chart);

   nv.utils.windowResize(chart.update);
   return chart;
  });
};

</script>

</body> </html> ' scrolling='no' seamless></iframe>

<style>iframe{ width: 100%; height: 400px;}</style>

Embed Option 2: Inline

This option embeds the chart inline in the html. It should work in all browsers that the charting library being used supports. However, it is susceptible to css and js conflicts.

n1$show('inline', include_assets = TRUE, cdn = TRUE)
<script type='text/javascript' src=http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js></script> <script type='text/javascript' src=http://d3js.org/d3.v3.min.js></script> <script type='text/javascript' src=http://timelyportfolio.github.io/rCharts_nvd3_tests/libraries/widgets/nvd3/js/nv.d3.min-new.js></script> <script type='text/javascript' src=http://nvd3.org/lib/fisheye.js></script> <style> .rChart { display: block; margin-left: auto; margin-right: auto; width: 600px; height: 400px; } </style>
<script type='text/javascript'> $(document).ready(function(){ drawchart13be6261be152() }); function drawchart13be6261be152(){ var opts = { "dom": "chart13be6261be152", "width": 600, "height": 400, "x": "Hair", "y": "Freq", "group": "Eye", "type": "multiBarChart", "id": "chart13be6261be152" }, data = [ { "Hair": "Black", "Eye": "Brown", "Sex": "Male", "Freq": 32 }, { "Hair": "Brown", "Eye": "Brown", "Sex": "Male", "Freq": 53 }, { "Hair": "Red", "Eye": "Brown", "Sex": "Male", "Freq": 10 }, { "Hair": "Blond", "Eye": "Brown", "Sex": "Male", "Freq": 3 }, { "Hair": "Black", "Eye": "Blue", "Sex": "Male", "Freq": 11 }, { "Hair": "Brown", "Eye": "Blue", "Sex": "Male", "Freq": 50 }, { "Hair": "Red", "Eye": "Blue", "Sex": "Male", "Freq": 10 }, { "Hair": "Blond", "Eye": "Blue", "Sex": "Male", "Freq": 30 }, { "Hair": "Black", "Eye": "Hazel", "Sex": "Male", "Freq": 10 }, { "Hair": "Brown", "Eye": "Hazel", "Sex": "Male", "Freq": 25 }, { "Hair": "Red", "Eye": "Hazel", "Sex": "Male", "Freq": 7 }, { "Hair": "Blond", "Eye": "Hazel", "Sex": "Male", "Freq": 5 }, { "Hair": "Black", "Eye": "Green", "Sex": "Male", "Freq": 3 }, { "Hair": "Brown", "Eye": "Green", "Sex": "Male", "Freq": 15 }, { "Hair": "Red", "Eye": "Green", "Sex": "Male", "Freq": 7 }, { "Hair": "Blond", "Eye": "Green", "Sex": "Male", "Freq": 8 } ]
  if(!(opts.type==="pieChart" || opts.type==="sparklinePlus")) {
    var data = d3.nest()
      .key(function(d){
        //return opts.group === undefined ? 'main' : d[opts.group]
        //instead of main would think a better default is opts.x
        return opts.group === undefined ? opts.y : d[opts.group];
      })
      .entries(data);
  }
  
  if (opts.disabled != undefined){
    data.map(function(d, i){
      d.disabled = opts.disabled[i]
    })
  }
  
  nv.addGraph(function() {
    var chart = nv.models[opts.type]()
      .x(function(d) { return d[opts.x] })
      .y(function(d) { return d[opts.y] })
      .width(opts.width)
      .height(opts.height)
     
    
      
    

    
    
    
  
   d3.select("#" + opts.id)
    .append('svg')
    .datum(data)
    .transition().duration(500)
    .call(chart);

   nv.utils.windowResize(chart.update);
   return chart;
  });
};
</script>
---
title : Embedding rCharts
author : Ramnath Vaidyanathan
framework : minimal
highlighter : prettify
hitheme : twitter-bootstrap
mode : standalone
---
<link href='http://fonts.googleapis.com/css?family=Lora' rel='stylesheet' type='text/css'>
<style>
p {font-family: "Lora"; text-align: justify;}
ol.linenums{margin-left: -5px;}
</style>
## Embedding rCharts in R Markdown
by [Ramnath Vaidyanathan](http://github.com/ramnathv), Dec 22, 2003
This is a short note to address a question from [Sharon Machlis](@sharon000) on the best way to embed a visualization created using [rCharts](http://rcharts.io), so that one can create a standalone HTML file using `knit2html`. If you have not used rCharts, you can try it out using the [online playground](http://rcharts.io/playground)
<blockquote class="twitter-tweet" lang="en"><p><a href="https://twitter.com/ramnath_vaidya">@ramnath_vaidya</a> Is this still best practice for knit2html to create rCharts file for use off your local machine? TNX! <a href="http://t.co/10RfiH9XgG">http://t.co/10RfiH9XgG</a></p>&mdash; Sharon Machlis (@sharon000) <a href="https://twitter.com/sharon000/statuses/414743096279441408">December 22, 2013</a></blockquote>
<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
You will need the latest version of `rCharts` (> 0.4) to use the approaches suggested in this note.
```{r eval = F}
library(devtools)
install_github("rCharts", "ramnathv")
```
---
## Create Chart
Let us first create a simple barplot using NVD3
```{r chart1, tidy = F}
library(rCharts)
hair_eye_male = subset(as.data.frame(HairEyeColor), Sex == "Male")
n1 <- nPlot(Freq ~ Hair, group = 'Eye',
data = hair_eye_male, type = 'multiBarChart'
)
n1$set(width = 600)
```
We need to set the chunk options `comment = NA` and `results = "asis"` so that the resulting html is rendered asis and not marked up (which is the default in knitr). Make sure to set `cache = F` for this chunk so that it is always run.
```{r setup, cache = F}
library(knitr)
opts_chunk$set(comment = NA, results = 'asis', comment = NA, tidy = F)
```
---
## Embed Option 1: IFrame Inline
The first option is to embed the chart as an inline iframe. It has the advantage of keeping the html standalone, but isolating the chart from the html on the page, thereby avoiding css and js conflicts. However, this feature is [not supported by IE and Opera](http://www.w3schools.com/tags/att_iframe_srcdoc.asp).
```{r chart2}
n1$show('iframesrc', cdn = TRUE)
```
---
## Embed Option 2: Inline
This option embeds the chart inline in the html. It should work in all browsers that the charting library being used supports. However, it is susceptible to css and js conflicts.
```{r chart3}
n1$show('inline', include_assets = TRUE, cdn = TRUE)
```
@timelyportfolio
Copy link

just noticed that the date is 2003 when I know it is 2013

@vashts85
Copy link

vashts85 commented Apr 8, 2015

This approach does not seem to work with a Morris chart, are you able to suggest any workarounds for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment