KPI dashboard in R with animated icons

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)

  10 comments for “KPI dashboard in R with animated icons

  1. 2016/02/13 at 08:03

    Hey Amit!

    Cool, thanks for sharing!

    Have not you tried Shiny for this?

    • Amit
      2016/02/13 at 23:50

      I’m trying to stay away from Shiny a bit… I anyway am working with beneficiary data which is confidential and I prefer to keep my data local. But yeah, I’ve messed around with shiny a bit. Also with shinydashboard… it’s really cool, but it’s annoying that hit FORCES me to use a header and left-menu… I need to embed my dashboard in sharepoint so I’m seeing how far I can get without it. Figured out another cool thing…you’ll see in an upcoming post. 🙂

      • 2016/02/14 at 08:46

        I don’t understand what is the problem with confidentiality in Shiny. With Shiny package you run it locally and by default it doesn’t accept external connections. Also you can use standalone Shiny server and it is possible to make authentication of users there even with open source edition.

      • 2016/02/14 at 08:50

        As about header and left-menu, you don’t have to use it. Any Shiny function from UI parts returns pure html code.

        For example:
        > mainPanel()

        > h1(“Header”)
        Header

        So you can use them in any combination:

        ************
        library(shiny)

        # Adopted from http://shiny.rstudio.com/articles/single-file.html

        server <- function(input, output) {
        output$distPlot <- renderPlot({
        hist(
        rnorm(input$obs),
        col = 'darkgray',
        border = 'white')
        })
        }

        ui <- mainPanel(sliderInput(
        "obs",
        "Number of observations:",
        min = 10,
        max = 500,
        value = 100),
        plotOutput("distPlot")
        )

        shinyApp(ui = ui, server = server)

        ************

        • 2016/02/14 at 08:52

          Sorry, output of mainPanel() and h1(“Header”) was corrupted in the comment as it is html code 🙂

      • 2016/02/14 at 08:54

        That is very interesting about integration of a dashboard in Sharepoint. Please provide more details about it!

        • Amit
          2016/02/14 at 19:13

          The shiny app needs to be deployed to the web somewhere… Unless it’s a server inhouse I feel uncomfortable. I might deploy to a server inhouse… I’m just to busy to work it out for now. And on your code… That’s shiny, I was talking about shinydashboard… Look it up. On dashboard in sharepoint, are you trying? If so, where are you stuck?

          • 2016/02/15 at 14:42

            In shinydashboard you can disable header and sidebar with `disable = TRUE` in dashboardHeader() and dashboardSidebar().

            If you are to present slides only inside of your network, try to run you Shiny app with runApp(…, host = “0.0.0.0”) and leave it running. R/Shiny on your computer will listen for external connections. And it will be possible (I hope) to include your Shiny app in the slides as external Internet web-page. You will need to know your local IP.

            I haven’t tried with Sharepoint. I just remember my tiny experience of using it in FAO 🙂

          • Amit
            2016/02/16 at 11:58

            Oh! That’s a convenient trick to `disable=TRUE`… thanks! Unfortunately, I would need about 40 dashboards running 24/7, for multiple projects… If I install shiny on a server of my own server, would I be able to run all 40 from that installation? Or does each require their own server? I know I could also deploy them to shinyapps.io (which I’m nervous about because of the beneficiary data).

            I’m almost done with the next piece of the html independent dashboard… you’ll see tommorrow!

  2. 2016/02/16 at 16:29

    Yep, I think it is possible to run 40 apps with open source edition of Shiny server. The question is how many users will simultaneously access these apps.

Leave a Reply

%d bloggers like this: