I’ve always liked the jQuery Sparklines library and today I had a use case for implementing these in one of my Rmarkdown reports.

While it wouldn’t be too difficult to staticly include a javascript based chart, ideally I would simply want to dynamically generate the sparkline using values computed in R. Luckily we now have htmlwidgets for R. This library makes it stupidly simple to integrate javascript libraries in R code. It simply up to one who has a use case for integrating a javascript library with R to insert some glue code: and so sparklines was born.

Usage is quite straightforward. I’ll retrieve some weatherdata for Brussels to populate a few sparkline charts.

weatherset <- getWeatherForDate("BRU", start_date = "2015-03-15", end_date = "2015-04-09", opt_verbose=F)
weather_values <- as.vector(weatherset$Mean_TemperatureC)

Let’s have a look at the temperature data.

##  [1]  4  5  8  6  6  6  6  4  4  5  4  3  6  7 10  7  9  7  6  4  6  5  4
## [24]  6  8 10

Now for the sparklines:

sparkline(weather_values, "line")

sparkline(weather_values, "bar")

sparkline(weather_values, "tristate")

sparkline(weather_values, "box")

Almost all configuration options and chart types as specified at http://omnipotent.net/jquery.sparkline/#s-docs are available. And, as a nice side note, it is also possible to use it in combination with Shiny :)

Installation instructions, configuration options, sourcecode and other documentation is available at https://github.com/Bart6114/sparklines.