By Tony Hirst
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…
Firstly, I had a go at popping some “base” hexjson files into a location within the package from which I could load them (checkin). Based on a crib from here, which suggests putting datafiles into an
extdata folder in the package
inst/ folder, from where
devtools::build() makes them available in the built package root directory.
With the files in place, we can use any base hexjson files included included in the package as the basis for hexmaps.
I also added the ability to switch off labels although later in the day I simplified this process…
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
rlist::merge()function. Here's the gist of the trick I ended up with, which was to construct a list
split()from each row in a dataframe, with the rowname as the list name, using
ll=lapply(split(customdata, rownames(customdata)), as.list) jsondata$hexes = list.merge(jsondata$hexes, ll)
hexjsondatamerge(hexjson,df) function takes a hexjson file and merges the contents of the dataframe into the hexes:
The contents of a dataframe can also be merged in directly when creating a hexjsonwidget:
Having started to work with dataframes, it also seemed like it might be sensible to support the creation of a hexjson object directly from a dataframe. This uses a similar trick to the one used in creating the nested list for the merge function:
As you might expect, we can then use the hexjson object to create a hexjsonwidget:
A hexjsonwidget can also be created directly from a dataframe:
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
hexjsonwidget()function, in effect overloading them so the same named parameter could be used in different supporting functions.
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!)
To leave a comment for the author, please follow the link and comment on their blog: Rstats – OUseful.Info, the blog….
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...
Source:: R News