Keegan Hines

Shiny Apps and ggmaps

I'm going to describe my new favorite toy from the R community: the Shiny package. This package makes it super easy to create awesome interactive visualizations using web browser-based applications. Importantly, all the potentially difficult stuff that is required to yield interactive web browser experiences (ie. javascript) is abstracted away from the user of Shiny, so that only some simple R code needs to be written, and all the rest is taken care of behind the scenes. Also, the resulting web application can be hosted by the Rstudio folks so you can share your visualization with the world. To me, that aspect makes this most exciting since it opens up the potential for all kinds of interactive experiences for the communiction of scientific ideas.

Generally, I'll be excited to use this tool both for teaching and for the presentation of data, but this example just showcases the basic things that can be done. This application simply generates pretty maps of any location you like, but it shows off how all your favorite R packages can be incorpated into such interative apps. In particular, this app calls upon the packages ggmaps and maptools to fetch data from GoogleMaps and render the spatial data using the watercolor aesthetic from Stamen Design. The resulting application is hosted at rstudio . Note: this particular app is pretty slow because it makes calls to external servers and it renders some computationally extensive images, etc, so it's just barely "interactive". But for most things, the time involved with the actual R computations will be negligible, so the experience will be much more interactive in real time.

It has fairly simple functionality and interactivity, but shows off the basic moving parts of Shiny apps. You can input a location string for the place that you want a map of. Generally, these location strings work best in the form of "City, State", but ultimately the string is sent to the GoogleMaps API, so anything you can search in GoogleMaps can be pulled into this app. You can also choose a zoom level - one of the 20 levels that are allowed by GoogleMaps. I generally find that these map tiles from Statmen tend to look the coolest if there are significant water features to be visualized, so a zoom level that captures a large geographic region looks best.

If you want to focus on a specific neighborhood or address, you can use the latitude and longitude coordinates. To find them, you can look around at GoogleMaps for the loction of interest, then if you right click and select "Whats Here", the lat/lon coordinates will appear in the search bar. You can then put them into the Shiny app and adjust the zoom level to pull up an awesome visualization of your neighborhood. NOTE: Due to some real lazy effort on my part, in order to use the lat/long input, the location string MUST read "Richmond, VA"; this was a silly choice on my part, I'll fix it later. If you like what you see, you can click Download Map to get a pdf.

The tutorials and documentation for Shiny are some of the best in the R community, so I highly recommend you walk through the first few of them. You'll have a strong grasp on the basic concepts in under a half an hour. The high level idea is to write a UI script which defines the components with which the user interacts and a Server script which describes what should be done with user input. The server script is where most of the calls to intersting R functions will go which will act upon user-supplied variables.

For example, the script running this app works something like this. The user supplies a location string which gets wrapped up in a variable called input . Then the server script uses that user variable and makes appropriate calls to ggmap functions such as,


MapCenter<-geocode(input$location) 
map<-get_map(c(lon=MapCenter$lon,lat=MapCenter$lat),maptype='watercolor',source='stamen')
print(gmap(map))

(A nice writeup of using ggmap can be found at SpatioAnalytics ). This plot is then sent back to the UI and displayed in the browser in the appropriate place.

Finally, I'm excited about encorporting Shiny apps into interactive documents and presentations. For example, if I am using html slide shows (made easy with the slidify package), or browser-based papers, reports, or blog posts, then I can easily include a shiny app using an iframe tag which points to wherever the app is hosted. For example,

Now the passive experience of reading a paper with static text and imagery becomes more immersive as the viewer can interact with the story you're telling and explore the concepts on their own.

EDIT : I guess there was a package update at ggmaps and now this app is broken. I'll fix it soon...