Building Interactive HTML Maps with R and Alteryx


map

a static screenshot – the end result is interactive

First a big thanks to Alteryx CTO Ned Harding for his recent posts on dynamic HTML rendering (here and here) and Scott Chamberlain from http://ropensci.org for his recent post (here) who have both really done the hard work in pulling together the technicalities for what follows. I’ve just merged the two ideas.

Firstly let’s start at the end, what are we producing? Well in essence we’re going to be building the ability to embed maps like this in your Alteryx HTML reports showing your polygon data. In doing so we’re going to be using R, not in the traditional way for number crunching, but using some of the packages that exist for doing all sorts of other cool stuff (this is the great thing about R, lots of people are building all sorts of crazy stuff for it).

If you want to cut to the chase and get going then first there’s a few steps you’ll need to follow first to set things up:

1.Install some packages for R that we’ll need, by far the easiest way to do this is via R itself, so boot up R (you’ll find it in your Program Files menu assuming you’ve installed it via the Alteryx Help menu). On Windows 7 don’t forget to run this using Administrator rights so you can install packages. Now run this code in the console:

install.packages(“devtools”)
library(devtools)
install_github(“rgbif”, “ropensci”, ref=”newapi”)
install.packages(“RCurl”)

You will have to select a mirror (i.e. where you want to download from – choose somewhere nearby) and then it will install what you need.

2. Set up a github account at https://github.com/. Make a note of your username (case-sensitive) and password. Github is a site where developers can share and collaborate on projects but we want to use some of its features to host our map.

3. Tell R about your GitHub account. Open the file C:\Program Files\Alteryx\R-3.0.1\etc\Rprofile.site (or similar depending on your install) in Notepad.

Now add two lines at the bottom:

options(github.username = “<github username>”)

options(github.password = “<github password>”)

Replacing <github username> and <github password> with your details from Step 2.

4. (optional – do you feel lucky Punk?) While you’re in R now might be is a good time to test whether you’ve set everything up correctly. You can do this by following the instructions under Make a Map at http://ropensci.org/blog/2013/10/23/style-geojson-polygon/

Now you’ve done that why not download my example module and have a play using the macro in it (remember to change the questions in the macro to your own details), add your own input shape files and create your reports. Note: We’re uploading the data to the internet – so make sure there’s nothing sensitive you wouldn’t want to share, also anything big probably won’t display so either generalise (that Generalise tool you never used? Now’s the time!) or pick a smaller data set.

Any problems let me know @ChrisLuv on twitter or via comments below.

Technicalities

Well the R packages do a lot of the work for us, I’ve just more or less copied the code from Scott’s example into the R tool in Alteryx and written the results back into the Alteryx data stream but there’s some things to be aware of:

  • I’ve used the “Block until Done trick” mentioned by Ned in one of his recent posts to ensure I write out a temporary shp file before it’s used in R. Lesson: read Ned’s blog, he’s a genius.
  • I could have output to geojson using Alteryx – but after a little while trying it became obvious the R method was better, the R function to export to geojson is probably a bit less limiting than Alteryx’s.
  • I can’t be sure what the field name is coming out of Alteryx (it’s named after the new gist id in Github), so I’ve just renamed it using a simple union tool “trick”. The dynamic rename tool would have been equally as good.
  • Once I’ve built up the HTML for embedding the map I’ve used Ned’s recent macro for dynamically building passthrough HTML.
  • I’ve used some actions to ensure other users can use the macro as is.

So there it is, hopefully it works. As ever it’s been thrown together and hardly tested so it’s certainly not fit for production use. Hopefully it presents a few more ideas for dynamic HTML and the options that R opens up outside traditional analysis.

Advertisements

One thought on “Building Interactive HTML Maps with R and Alteryx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s