<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/r-d3-a-powerful-and-beautiful-combo/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://rmnppt.com/2017/03/16/htmlwidget-example.html"> Adventures in Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>For a while now, I have been looking for ways to incorporate pretty D3 graphics into R markdown reports or shiny apps. Why? I like R for the ability to be able to do data handling, stats, ML all very easily with minimal code. But when you want to present something to clients or the public, there is no competition for the front end web stuff e.g. d3.js. The answer: <em>htmlwidgets</em>.</p>
<p><a target="_blank" rel="nofollow" href="https://thedatalab.shinyapps.io/rc3_shiny_test/">Here is my attempt so far</a></p>
<p>Ok well I wasn’t looking too hard because it completely escaped me that you could do this with <a target="_blank" rel="nofollow" href="http://www.htmlwidgets.org/">htmlwidgets</a>. I stumbled upon this when I was browsing the <a target="_blank" rel="nofollow" href="https://www.rstudio.com/products/shiny/shiny-user-showcase/">shiny user showcase</a> and I came across the <a target="_blank" rel="nofollow" href="https://frissdemo.shinyapps.io/FrissDashboard/">FRISS Analytics example</a> which is accompanied by a really <a target="_blank" rel="nofollow" href="http://shiny.rstudio.com/tutorial/">fantastic set of tutorials</a> under ‘other tutorials’. If you want to do this to customise for your own plotting needs, I strongly suggest you start there with those tutorials. They are very thourough and step-wise and will allow you to build a R-D3 binding for the first time with very little hassle, as I have done.</p>
<p>It looks a little like this…</p>
<ul>
<li>You write a package.</li>
<li>The package contains R functions.</li>
<li>Those functions invoke JavaScript as you define it.</li>
<li>htmlwidgets binds them together and provide the plumbing between your R objects and the JavaScript that you feed them into.</li>
</ul>
<p>The outcome is that you can easily create an interactive graphic in the end my simply calling an R function. <strong>The real beauty</strong> though is being able to update the javascript plot in response to user input on the R side, without plotting a new plot each time in the slightly awkward way that I was previously doing. If you have loaded the app try typing extra zeros into the sample size, you’ll see the plot update as the underlying data is updated. Smooth. This is what I was looking for.</p>
<p>Of course you don’t need to be a JavaScript programmer to get this done. You can use higher levels js libraries such as <a target="_blank" rel="nofollow" href="http://rmnppt.com/2017/03/16/c3js.org">C3</a> in my case or <a target="_blank" rel="nofollow" href="http://rmnppt.com/2017/03/16/nvd3.org">nvd3</a>, maybe there are more?</p>
<p>So all in all the chain is…</p>
<p><code>R -> htmlwidgets -> || C3 -> D3 -> JavaScript.</code></p>
<p>Where htmlwidgets is reaching through the border between R and JavaScript.</p>
<p><a target="_blank" rel="nofollow" href="https://github.com/rmnppt/rc3">I forked the R package</a> from FRISS and when I get some time or there is a need, I will try to port some more C3 based templates and expose them to R functions in this way.</p>
<p>This post is obviously not a tutorial just a flag and a signpost, to find out how to do this yourself, go to the FRISS Analytics tutorial either <a target="_blank" rel="nofollow" href="http://shiny.rstudio.com/tutorial/">here on rstudio</a> or <a target="_blank" rel="nofollow" href="https://github.com/FrissAnalytics/shinyJsTutorials">here on github</a>. Thanks a million to the <a target="_blank" rel="nofollow" href="https://github.com/FrissAnalytics/shinyJsTutorials/graphs/contributors">folks at FRISS analytics</a> and the <a target="_blank" rel="nofollow" href="https://github.com/ramnathv/htmlwidgets/graphs/contributors">authors of htmlwidgets</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://rmnppt.com/2017/03/16/htmlwidget-example.html"> Adventures in Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/Wu070rVTA6E"></a></div>