All examples By author By category About

timelyportfolio

jquery.sparkline in rhandsontable with NA

Built with blockbuilder.org and R htmlwidgets


NULL/NA in rhandsontable sparkline

Prompted by #rstats friend @TimSalabim3 I recently remembered that the htmlwidget rhandsontable gives us sparklines in cells through a jquery.sparkline renderer. These sparklines can digest NA values from R, but just need one argument na="null" added to the jsonlite::toJSON call.

library(rhandsontable)

DF = data.frame(int = 1:10,
                numeric = rnorm(10),
                logical = TRUE,
                character = LETTERS[1:10],
                fact = factor(letters[1:10]),
                date = seq(from = Sys.Date(), by = "days", length.out = 10),
                stringsAsFactors = FALSE)

# add a sparkline chart
DF$chart = sapply(1:10, function(x) jsonlite::toJSON(list(values=rnorm(10))))

# add some NAs to our chart data
#   note: the na="null" is the important piece
DF$chart[1] <- jsonlite::toJSON(list(values=c(1,2,3,4,5,NA,NA,8,10)),na="null")

rhandsontable(DF, rowHeaders = NULL) %>%
  hot_col("chart", renderer = htmlwidgets::JS("renderSparkline"))

live example

screenshot of sparkline line with na