Getting Started with R Shiny
Interactive Visualisations in R
This post was inspired by this competition, hosted by JumpingRivers. The competition asks you to extract data from their GitHub account containing the details of R groups and R ladies groups around the world. With this data it asks you to create a visualisation, of any kind. I’ve been working on my visualisation skills in R so I thought I would have a go at entering.
Geographical data always works well plotted on a map and since there is a lot of information to visualise from all over the world, I decided to incorporate an element of interactivity. The easiest way to do this using R is through the RShiny package. Therefore this project was made up of 2 steps: extracting the data to get it in a form to be visualised and then creating an interactive Rshiny app showing the locations of the different groups on a map.
To Extract the Data:
The files on github can be read into R as character vectors (where each line is a element of the vector) using the R “readLines” function.
From this character vector, we need to extract the country, the group name and url. This can be done by recognising that each line containing a country starts with a ‘##’ and each line containing the group name and url starts with a ’*‘. Therefore we can use these ’tags’ to cycle through each element of the character vector and pull out vectors containing the countries, the cities and the urls of the R groups. These vectors can then be cleaned and joined together into a data frame.
I wrote these steps into a function that accepted each R group character vector as an input and returned the final data frame. As one of the data sets contained just R Ladies groups, I fed this in as an argument and returned it as a column in the final data frame in order to differentiate between the different group types. I also returned a variable based on the character vector input in order to differentiate between the different world continents.
Running this function on each of the character vectors creates separate data sets which can then be all joined together. This creates a final dataset containing all the information on each R group: the type of group, the url, the city and the region.
To Visualise the Data
For the visualisation I used the leaflet package to plot world maps entered on different continents. I then plotted the locations of different R groups depending on which groups had been selected by the user. One of the issues I found was trying to use the user input to trigger the correct version of the code to run. I got stuck trying to create for loops with the user inputs and in the end I wrote a separate section of code for each possible user input which is probably not the most efficient way. However it works which was the most important thing!
There are then different ways you can launch your app after they have been created. The simplest way for me was to use the shinyapps.io website. Following the site instructions I launched my app which can be found at https://jennifersnape.shinyapps.io/leaflet_app . My app is fairly basic in terms of the website design. There are ways to make it look more professional by adding html into your Shiny code but this is something that requires a little bit more time to learn and is not required if you just want to explore data.
So my visualisation did not win the Jumping Rivers competition.However it was not a complete waste of time because it was a good excuse for me to learn R shiny and I did actually get a prize for the secondary competition for my GitHub extraction code. See this blog for details on the winners of the competition and my extract code. !