sparklines for R

[This article was first published on FishyOperations, 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.

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.

library(weatherData)
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.

weather_values
##  [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:

library(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.

To leave a comment for the author, please follow the link and comment on their blog: FishyOperations.

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)