[Tutorial] R on Shiny: A web-mapping solution from RStudio

To begin with, as this is the first post of 2016, Happy New Year everyone..!!

This post will be talking on how can someone easily build a web-map with RStudio and the web framework that they released for R, Shiny.

The benefits of this method for web-mapping is that R on Shiny is offering a quick and easy solution to people that don’t have a know-how on web development.

What we are going to build is a very simple web-map, using R, Shiny, and leaflet. The things that we are going to do is to load a basemap and the geolocated data that we want to show, add a specific level of scale/zoom in the area and buttons for activation/deactivation of the geodata, and the interaction that they can have with the mouse (information for the polygon on request, by clicking or hovering).

Data Management

For better integration of the geodata with our web map, I converted the polygon shapefile I am going to use in GeoJSON format. This format enables a better usage from programming languages like R, and an easier way of managing it (one big file, instead of multiple smaller files that compose a shapefile). You can use any GIS software to make this conversion, and you can also choose the equivalent coordinate system of the area you are going to map; in this case of London Boroughs, I used the British National Grid (EPSG: 27700). There will be a transformation applied later on, as the basemap we will use will be in the WGS84 (EPSG: 4326), so just make to use a coordinate system that can easily be translated in the later system.

Installation of the packages

So, assuming that you have R and RStudio installed on your machine, you are going to create a folder, in which you are going to place 2 files that you will create: ‘ui.R’ and ‘server.R’. These two scripts will enable the user interface and the server respectively for our web map. There are 4 packages we have to install and are going to need for this web-map: shiny, leaflet, rgdal, sp.
shiny is a library that will allow an automated “reactive” binding of inputs and outputs, which will make applications possible with minimal effort.
leaflet is an open-source library built in JavaScript, used for interactive mapping.
The latter 2 packages, rgdal and sp, are the ones that give our program the possibility to import our geodata.
These packages are going to be imported in both of our R files.

ui.R, server.R

Setting up the ui.R

This part will allow us to code the UI for our application.
As this application is going to be just for the basics, we want our to include only the essentials of a web-map. Thus, the elements we are going to add are a title, a checkbox, in order to be able to open and close the layer with our data, and the interactive map itself. The dimensions of the webmap should be able to cover a 720p screen.

titlePanel("London Boroughs"),
checkboxInput("boroughs", "Boroughs", value = FALSE),
leafletOutput('myMap', height = 800, width = 1200)

This is all the code that someone would need to code the UI of such a webmap. Pretty amazing, huh?

Setting up the server.R

To begin with our server, before setting the stage for the shiny server, we would need to import our GeoJSON. The functions we will use are the readOGR, in order to import the GeoJSON, and the spTransform, so that we would reproject the geofile to the coordinate system of our basemap (WGS84, EPSG: 4326).

boroughPoly <- readOGR("./data/LondonBoroughs.geojson", "OGRGeoJSON")
boroughPoly <- spTransform(boroughPoly, CRS("+init=epsg:4326"))

So now the geofile we imported is stored within boroughPoly. The next step is to setup the server for our shiny web map. Pretty easily, we are going to add the leaflet basemap, zoomed in the area of London. Following to this, we are going to add a reactive function with a conditional statement, so that when we click the checkbox we inserted in the UI, to activate/deactivate the map. If the function is activated, then our layer is added on top of the basemap, in colours and stroke of our choice, as well as the option of an interactive popup to be shown each time we click on one of the polygons of our geofile; which in this case will be showing the name of the Borough.

shinyServer(function(input, output) {
map = leaflet() %>% addTiles() %>% setView(-0.12, 51.5, 10)
finalMap <- reactive ({
if(input$boroughs) return(map %>% addPolylines(data = boroughPoly, stroke = TRUE, fillColor = "blues", fill = TRUE, popup = ~name))
else return (map)
output$myMap = renderLeaflet(finalMap())

The result will be something like the one shown in the following link: https://purplexed.shinyapps.io/try_leaflet/
You can also find the full code and the data used in this tutorial here.

That’s it! With a few lines of code, anyone can create a web map using R and Shiny. You can deploy this in a host server of your choice, one of which can be shinyapps; you can “publish” this with a click of a button from RStudio directly.

One of the thing that makes web-mapping with R on Shiny less appealing is the fact that there are not a lot of adjustments that someone could play around with, like someone could do with a JavaScript-based webmap.

That’s all, happy mapping!!

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s