This sparkline htmlwidget is based on the nifty jQuery Sparklines.
htmlwidgets for R has made it extremely easy to integrate, access and use html widgets from R.
Usage is quite straightforward. First install the package using devtools
.
devtools::install_github("Bart6114/sparklines")
After installation you’re ready to go. Let’s get some data on the daily results of the GOOG stock.
library(quantmod); library(dplyr)
GOOG <-
getSymbols("GOOG", src = 'yahoo', from = '2015-03-15', env = NULL) %>%
as.data.frame %>%
mutate(day_result = GOOG.Close - GOOG.Open)
GOOG_daily_result <-
as.vector(GOOG$day_result)
GOOG_daily_result
## [1] 3.56 -0.87 7.00 -1.40 -1.29 -1.62 7.63 -11.72 -2.42 -4.66
## [11] 0.41 -2.00 -6.04 -5.32 4.55 -1.06 3.23 -0.25
Now show a nice sparkline:
library(sparklines)
sparkline(GOOG_daily_result, "line")
We can also configure the look and feel a bit using a named list with options such as defined at http://omnipotent.net/jquery.sparkline/#s-docs. For example:
sparkline(GOOG_daily_result, "line", list(fillColor="white"))
Different types of charts are available in the jQuery Sparklines library. A few of them are shown below.
sparkline(GOOG_daily_result, "line")
sparkline(GOOG_daily_result, "bar")
sparkline(GOOG_daily_result, "tristate")
sparkline(GOOG_daily_result, "box")
You can also use sparklines in a Shiny app. Just test the following minimal example and roll from there.
library(shiny)
library(sparklines)
server <- function(input, output) {
output$spark <- renderSparkline({
sparkline(runif(input$values, -5, 5), input$chart_type)
})
}
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("values", "Number of values:", min = 5, max = 100, value = 10),
selectInput("chart_type", "Chart type", choices = list("line","bar","tristate","box"))
),
mainPanel(sparklineOutput("spark"))
)
)
shinyApp(ui = ui, server = server)
Type ?sparkline
to open up the function’s R documentation.
Almost all chart types and configuration options, as defined at http://omnipotent.net/jquery.sparkline/#s-docs, can be used.
If you encounter any issues, please open an issue at the sparklines GitHub repository.