KPI dashboard in R with animated icons

[This article was first published on R – AmitKohli.com, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

So Key Performance Indicators (KPIs) are all the rage in the dashboarding community… well everywhere really. The premise is simple… check a list of measurements against targets and show how they compare using some kind of visualization. I haven’t yet seen, however, a version that can utilize animated icons to display indicators that REALLY need attention. So here you go, a tutorial on how to make your very own animated icon KPI, using the googleVis library.

Suppose we have a dataset that looks like this (make sure to set your working directory):

library(googleVis) ## Set wd setwd("your folder") df <- data.frame(thing=paste("Item",1:15), measure=round(runif(15,max=10),2), target=round(runif(15,max=10),2)) ## unimpresive dashboard plot(gvisTable(df))

data

A normal KPI would then compare the measure to the target and apply some rationale. Suppose in our case that green indicates that for that indicator you are within 80% of your target. Yellow means up to 50% of target, and red is below that.

Now, we need some icons. We can download them, or make them ourselves (protip: MS Powerpoint has some interesting possibilities with their glow/highlight/dropshadow options so this might be a good place to start if you’re not a graphic designer). Now that we have icons, we can split up the dataset into good, bad and medium categories and assign icons to each:

 

## Make more interesting one... first set target thresholds Threshold1 <- 0.8 Threshold2 <- 0.5 ## high trigger df$graphic[df$measure/df$target > Threshold1] <- '<img src="green.gif">' ## low trigger df$graphic[df$measure/df$target < Threshold2] <- '<img src="red.gif">' ## medium trigger df$graphic[is.na(df$graphic)] <- '<img src="yellow.gif">' plot(gvisTable(df))

ohnoes

Oh no! Why won’t it work? Relax, it’s because we are in the localhost. See how the address is http://127.0.0.1/… ? This isn’t a real webpage, it’s launching from your computer. We need to port out the right elements into a handy-dandy webpage (the code is very ugly, but SUPER flexible… you can pass css elements, titles, javascript… hell you can even create a fully functional webpage like this! I love this method, don’t bash it!):

##--- Make html ---- ObsRep <- gvisTable(df) # plot(ObsRep) cat(paste("<html><head></head><body>", "<h1>Best Dashborde!!!1!</h1>",sep=""), ObsRep$html$header, ObsRep$html$chart, "</body></html>", file="AnimatedKPIdashboard.html") browseURL("AnimatedKPIdashboard.html")

and now check it out:

better

 

Not bad… this is what most KPI indicator lights look like… But what if we REALLY want to call the attention to some items, say where the measure is less than 20% of the target, let’s bring out the big guns and assign a red flashing light to the terriblest:

Threshold3 <- 0.2 ## lowlowlowlowlow trigger df$graphic[df$measure/df$target < Threshold3] <- '<img src="redFlashing.gif">' ##--- Make html again ---- ObsRep <- gvisTable(df) # plot(ObsRep) cat(paste("<html><head></head><body>", "<h1>Best Dashborde!!!1!</h1>",sep=""), ObsRep$html$header, ObsRep$html$chart, "</body></html>", file="AnimatedKPIdashboard.html") browseURL("AnimatedKPIdashboard.html")

And voilà! Here’s our KPI with a flashing red light for the real underperformers:

(The link also here: >>AnimatedKPIdashboard<<)

And from here the sky is the limit! Enjoy re-discovering animated gifs! There’s a few gems out there.

As always, full code is in my github account.

 

(Editing by Laure Belotti)

To leave a comment for the author, please follow the link and comment on their blog: R – AmitKohli.com.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)