<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/hexjson-htmlwidget-for-r-part-3/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.ouseful.info/2017/06/30/hexjson-htmlwidget-for-r-part-3/"> Rstats – OUseful.Info, the blog…</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In <a rel="nofollow" href="https://blog.ouseful.info/2017/06/28/hexjson-htmlwidget-for-r-part-1/" target="_blank">HexJSON HTMLWidget for R, Part 1</a> I described a basic HTMLwidget for rendering hexJSON maps using <code>d3-hexJSON</code>, and <a rel="nofollow" href="https://blog.ouseful.info/2017/06/29/hexjson-htmlwidget-for-r-part-2/" target="_blank">HexJSON HTMLWidget for R, Part 2</a> described updates for supporting colour.</p>
<p>Having booked off today for emergency family cover that turned out not to be required, I had another stab at the package, so it now supports the following additional features…</p>
<p>Firstly, I had a go at popping some “base” hexjson files into a location within the package from which I could load them (<a rel="nofollow" href="https://github.com/psychemedia/htmlwidget-hexjson/commit/bb1c33239768a34cbae96c8c72313713fd891bce" target="_blank">checkin</a>). Based on a crib from <a rel="nofollow" href="http://r-pkgs.had.co.nz/data.html" target="_blank">here</a>, which suggests putting datafiles into an <code>extdata</code> folder in the package <code>inst/</code> folder, from where <code>devtools::build()</code> makes them available in the built package root directory.</p>
<pre>hexjsonbasefiles &lt;- function(){
  list.files(system.file(&quot;extdata&quot;, package = &quot;hexjsonwidget&quot;))
}</pre>
<p>With the files in place, we can use any base hexjson files included included in the package as the basis for hexmaps.</p>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_1.png?w=443&amp;h=503&amp;fit=443%2C503&amp;resize=443%2C503" data-orig-size="1130,1284" data-medium-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_1.png?w=443&amp;h=503&amp;fit=443%2C503&amp;resize=443%2C503?w=264" data-attachment-id="16553" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_1.png?w=443&h=503&fit=443%2C503&resize=443%2C503" data-permalink="https://blog.ouseful.info/2017/06/30/hexjson-htmlwidget-for-r-part-3/hexjsonpart3_1/" data-large-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_1.png?w=443&amp;h=503&amp;fit=443%2C503&amp;resize=443%2C503?w=830" alt="" data-image-title="hexjsonpart3_1" sizes="(max-width: 443px) 100vw, 443px" data-comments-opened="1" class="wp-image-16553" data-recalc-dims="1"></p>
<p>I also added the ability to <a rel="nofollow" href="https://github.com/psychemedia/htmlwidget-hexjson/commit/78cf79ae3c91c62a87076d0445bc50db45b55276" target="_blank">switch off labels</a> although later in the day I simplified this process…</p>
<p>One thing that was close to the top of my list was the ability to merge the contents of a dataframe into a hexJSON object. In particular, for a row identified by a particular key value associated with a hex key value, I wanted to map columns onto hex attributes. The hexjson object is represented as a list, so this required a couple of things: firstly, getting the dataframe data into an appropriate list form, secondly merging this into the hexjson list using the <code>rlist::merge()</code> function. Here’s the gist of the <a rel="nofollow" href="https://github.com/psychemedia/htmlwidget-hexjson/commit/b33c49e412850f42fdf7747b8c601387f7751130" target="_blank">trick</a> I ended up with, which was to construct a list <code>split()</code> from each row in a dataframe, with the rowname as the list name, using <code>lapply(.., as.list)</code>:</p>
<pre>ll=lapply(split(customdata, rownames(customdata)), as.list)
jsondata$hexes = list.merge(jsondata$hexes, ll)</pre>
<p>A <code>hexjsondatamerge(hexjson,df)</code> function takes a hexjson file and merges the contents of the dataframe into the hexes:</p>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_3.png?w=450&amp;h=326&amp;fit=457%2C326" data-orig-size="1452,1036" data-medium-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_3.png?w=450&amp;h=326&amp;fit=457%2C326?w=300" data-attachment-id="16551" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_3.png?w=450&h=326&fit=457%2C326" data-permalink="https://blog.ouseful.info/2017/06/30/hexjson-htmlwidget-for-r-part-3/hexjsonpart3_3/" data-large-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_3.png?w=450&amp;h=326&amp;fit=457%2C326?w=830" alt="" data-image-title="hexjsonpart3_3" sizes="(max-width: 457px) 100vw, 457px" data-comments-opened="1" class="wp-image-16551" data-recalc-dims="1"></p>
<p>The contents of a dataframe can also be merged in directly when creating a hexjsonwidget:</p>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_2.png?w=450&amp;h=332&amp;fit=477%2C332" data-orig-size="1494,1040" data-medium-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_2.png?w=450&amp;h=332&amp;fit=477%2C332?w=300" data-attachment-id="16552" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_2.png?w=450&h=332&fit=477%2C332" data-permalink="https://blog.ouseful.info/2017/06/30/hexjson-htmlwidget-for-r-part-3/hexjsonpart3_2/" data-large-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_2.png?w=450&amp;h=332&amp;fit=477%2C332?w=830" alt="" data-image-title="hexjsonpart3_2" sizes="(max-width: 477px) 100vw, 477px" data-comments-opened="1" class="wp-image-16552" data-recalc-dims="1"></p>
<p>Having started to work with dataframes, it also seemed like it might be sensible to support the <a rel="nofollow" href="https://github.com/psychemedia/htmlwidget-hexjson/commit/2349fdf5b039cb73c5d9e377e5e91c6ad414e192" target="_blank">creation of a hexjson object directly from a dataframe</a>. This uses a similar trick to the one used in creating the nested list for the merge function:</p>
<pre>hexjsonfromdataframe &lt;- function(df,layout=&quot;odd-r&quot;, keyid='id',
                                 q='q', r='r'){

  rownames(df) = df[[keyid]]
  df[[keyid]] = NULL
  colnames(df)[colnames(df) == q] = 'q'
  colnames(df)[colnames(df) == r] = 'r'

  list(layout=layout,
       hexes=lapply(split(df, rownames(df)), as.list))
}</pre>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_6.png?w=450&amp;h=179&amp;fit=487%2C179" data-orig-size="1124,414" data-medium-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_6.png?w=450&amp;h=179&amp;fit=487%2C179?w=300" data-attachment-id="16558" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_6.png?w=450&h=179&fit=487%2C179" data-permalink="https://blog.ouseful.info/2017/06/30/hexjson-htmlwidget-for-r-part-3/hexjsonpart3_6/" data-large-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_6.png?w=450&amp;h=179&amp;fit=487%2C179?w=830" alt="hexjsonpart3_6" data-image-title="hexjsonpart3_6" sizes="(max-width: 487px) 100vw, 487px" data-comments-opened="1" class="wp-image-16558" data-recalc-dims="1"></p>
<p>As you might expect, we can then use the hexjson object to create a hexjsonwidget:</p>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_4.png?w=409&amp;h=444&amp;fit=409%2C444&amp;resize=409%2C444" data-orig-size="950,1032" data-medium-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_4.png?w=409&amp;h=444&amp;fit=409%2C444&amp;resize=409%2C444?w=276" data-attachment-id="16550" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_4.png?w=409&h=444&fit=409%2C444&resize=409%2C444" data-permalink="https://blog.ouseful.info/2017/06/30/hexjson-htmlwidget-for-r-part-3/hexjsonpart3_4/" data-large-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_4.png?w=409&amp;h=444&amp;fit=409%2C444&amp;resize=409%2C444?w=830" alt="" data-image-title="hexjsonpart3_4" sizes="(max-width: 409px) 100vw, 409px" data-comments-opened="1" class="wp-image-16550" data-recalc-dims="1"></p>
<p>A hexjsonwidget can also be created directly from a dataframe:</p>
<p><img data-orig-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_5.png?w=441&amp;h=372&amp;fit=441%2C372&amp;resize=441%2C372" data-orig-size="1220,1028" data-medium-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_5.png?w=441&amp;h=372&amp;fit=441%2C372&amp;resize=441%2C372?w=300" data-attachment-id="16549" data-image-meta="{" data-image-description="" src="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_5.png?w=441&h=372&fit=441%2C372&resize=441%2C372" data-permalink="https://blog.ouseful.info/2017/06/30/hexjson-htmlwidget-for-r-part-3/hexjsonpart3_5/" data-large-file="https://ouseful.files.wordpress.com/2017/06/hexjsonpart3_5.png?w=441&amp;h=372&amp;fit=441%2C372&amp;resize=441%2C372?w=830" alt="" data-image-title="hexjsonpart3_5" sizes="(max-width: 441px) 100vw, 441px" data-comments-opened="1" class="wp-image-16549" data-recalc-dims="1"></p>
<p>In creating the hexjson-from-dataframe, I also refactored some of the other bits of code to simplify the number of parameters I’d started putting into the <code>hexjsonwidget()</code> function, in effect overloading them so the same named parameter could be used in different supporting functions.</p>
<p>I think that’s pretty much it from the developments I had in mind for the package. Now all I need to do is put it into practice… testing for which will, no doubt, throw up issues!)</p>
<p> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ouseful.wordpress.com/16548/" target="_blank"><img border="0" alt="" src="http://feeds.wordpress.com/1.0/comments/ouseful.wordpress.com/16548/"></a> <img border="0" alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" data-recalc-dims="1"></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="https://blog.ouseful.info/2017/06/30/hexjson-htmlwidget-for-r-part-3/"> Rstats – OUseful.Info, the blog…</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/1ioG5ZdapKo" height="1"></a></div>