Building Interactive Graphs with ggplot2 and Shiny Review

In 2009, I was asked to graph some data. It was not a lot but it was painful trying to get it done within the day. Back then there weren’t a lot of options for graphing data and I had to fix bugs of Google charts Ruby gem. Eventually, Matt added me as a maintainer for the repository and asked me to do something which I forgot. Back then I knew that Ruby is not very good for data analysis but it can suffice for data mining.

That gem actually still relied on JavaScript which is a little funny because some of us have habits of doing everything in Ruby and then it slows us down a bit (hopefully not several hours).

I’m writing this review of Building Interactive Graphs with ggplot2 and Shiny by Christophe Ladroue as it was requested by Packt Publishing. I usually review lengthy books which take at least a day of my life but now I find that videos take a lot less hours and are far more convincing than reading more than 200 pages of a book.

This review is just a little bit late due to other work. Glad I still did it as I learned a lot about R, particularly ggplot2 and Shiny. Both of these were created by Hadley Wickham, an Assistant Professor of Statistics at Rice University.

Before I give my verdict, I have some comments about the language I haven’t used a lot because it is not something we or other people need for a minimum viable product or “the last chance to save sanity in an increasingly unstable industry.”

Why learn R and something else other than programming?

R is used very well in the fields of Economics, Statistics and Finance. All of these are obviously related but with great differences on focus and complexity of work. Both Economics and Statistics are prerequisite subjects for Finance. R is more popular to people in the Finance industry because there are some books written like Portfolio Optimization with R/Rmetrics. It’s used a lot in portfolio analysis. I have been studying Computational Finance as a hobby (for now at least). I have no plans or interests other than to save my brain from being stuck to learning Ruby, CSS and JavaScript (or probably any language) which may take years to learn but are low level skills on their own if you think about it.

Consider this advice from Zed Shaw who just turned 40 a few days ago:

Programming as a profession is only moderately interesting. It can be a good job, but you could make about the same money and be happier running a fast food joint. You’re much better off using code as your secret weapon in another profession. People who can code in the world of technology companies are a dime a dozen and get no respect. People who can code in biology, medicine, government, sociology, physics, history, and mathematics are respected and can do amazing things to advance those disciplines. Of course, all of this advice is pointless. If you liked learning to write software with this book, you should try to use it to improve your life any way you can. Go out and explore this weird wonderful new intellectual pursuit that barely anyone in the last 50 years has been able to explore. Might as well enjoy it while you can.

That is from the book “Learn Python the Hard Way.”

Most of the programmers I have met (in person) are intelligent and respectable but many of them relate to me how they were treated otherwise. It’s no longer drama but a natural state because of the industry. They gave me very good advice as they are very senior (over a decade of experience) compared to me (who just managed to float from job to job). Despite having a background and interest in Finance, I will always empathize more with the computer scientist or software developer. Consider those words by Zed Shaw as priceless.

Initially, I did not believe it’s relevant for web developers to learn R until I watched the videos. It could save a lot of time if you knew you can do a few things more quickly with R language. Given a task of presenting several graphs, Shiny definitely shines as it applies reactive programming. When I first tried it, it was easy as creating a new Sinatra application and as interesting as Meteor.js because of reactivity being one of its core features.

The video tutorial was definitely worth the time. At least, I know I don’t have to spend several hours trying to figure out how to create different kinds of graphs and I don’t have to convert CSV data to JSON or XML to JSON. It can probably handle JSON with jsonlite as well.

Some Shiny examples

The last example was a bit like Google Public Data and it uses the same World Bank data (economic indicators).

I don’t want to pretend that I spent several days learning this. I just spent a few minutes today to create an example.

With Google fu, I found quantmod (Quantitative Financial Modelling & Trading Framework for R) and created a Shiny application which shows you graphs of different stocks based on Google Finance data.

Please take a look at the Github repository for “Shiny stocks.”

The video tutorial also covers deployment and how to show off the graphs to your friends but I have a few more tips below.

Installing R and R Studio

If you’re used to installing everything using Homebrew (which I am), please save yourself some time and move on to download R for your Mac version. Choose the latest build. Why not use Homebrew? Initially it will work but when you try to install packages, it won’t work at all. I assume this is because it’s the exact same patched version from the website.

R Studio is the free IDE which shows the plot and like Eclipse, it allows you to install the dependencies without using the console. Take time to learn some basic commands like clearing the console (CTRL + L). You would want to do that so it’s easier to see the output. Many things are fairly easy to figure out. For setting a new work directory, you can use:

setwd("/path/learning_ggplot2_and_shiny/Section 6") 

Or simply click on the “more” link on files tab which shows “Set As Working Directory” button.

Installing devtools

If you want to test some R programs on Github, you should install devtools package first to be able to use the command install_github.

install.packages("devtools")

runGithub does not Work

I tried the runGithub option for showing off work but it returned an error. I found this works and you can test it out.

On your terminal, run R Console or use the console on R Studio.

install.packages("shiny")
shiny::runUrl('https://github.com/kathgironpe/shiny-stocks/archive/master.zip')

You should see this:

Shiny example

Deploying on Heroku

I saw this repo Heroku Buildpack Shiny. Hope it helps!