R has been the perfect language for the back end of this government data dashboard I am developing.
- It has excellent packages to pipe in data from every significant source
- Tools like dplyr and tidyr make cleaning and munging data trivial
- It is ideal for automating analysis
In the R script that powers my dashboard, I have everything from simple averages and frequency tables, to a complex algorithm that converts timeseries figures to Z-Scores and then selects the top 3 variables to display based on standard scores from the last 7 days. (Today that happens to include an uptick in reports of dead animals on the streets and sidewalks, which is disgusting but useful to know).
Now this is what your index.Rhtml file will look like, in it’s most basic form. Here I am using Highcharts, but this could just as easily be D3 or any other charting library.
The parts to pay attention to are the ones in the HTML that say
rinline I(X). These are the values from your R analysis that will get “woven in” to regular HTML using the Rhtml file. Yihui Xie, knitr’s heroic creator, describes this method here.
I created a repository here that contains the simple example outlined above, plus the slightly redacted files that I use to power our dashboard. The great thing is that Yihui has created a system where you can easily combine the analytical horsepower of R with virtually any charting library.
Bonus: Automate It
So now you have an R file for the analysis, which ends by creating HTML and uploading it to your web server. There are, of course, dozens of ways to set things up so that your R script gets called each day, hour, or however often you want your dashboard to update. At home, I use cron on a Raspberry Pi. But at work, I have found that the Task Scheduler program on Windows accomplishes this painlessly. I just schedule it to run the following .bat file every night at around 2 am: “C:\Program Files\R\R-3.1.2\bin\R.exe” CMD BATCH –vanilla –slave “C:\Users\myPath\DailyChartMaker.R”