htmlwidgets / sparkline

jQuery Sparkline HTML Widget for R
Other
245 stars 40 forks source link

Fixing sparkline columns with null values #27

Open VictorNautica opened 5 years ago

VictorNautica commented 5 years ago

Not sure whether this package is still actively being maintained, but I noticed an issue where if some elements have no/null values in the values CSS selector/value, represened by null, then this prevents further sparkline elements from being generated despite having values.

An example:

library(sparkline)
library(formattable)

test_spark <- c("<span id=\"htmlwidget-9252a3086ccb0c5b1aa5\" class=\"sparkline html-widget\"></span>\n<script type=\"application/json\" data-for=\"htmlwidget-9252a3086ccb0c5b1aa5\">{\"x\":{\"values\":[7.04,7.31,7.7,8.32,8.78,9,9.16,9.14,9.14],\"options\":{\"type\":\"line\",\"height\":20,\"width\":60},\"width\":60,\"height\":20},\"evals\":[],\"jsHooks\":[]}</script>", 
              "<span id=\"htmlwidget-1e860d3afe832f38c6f3\" class=\"sparkline html-widget\"></span>\n<script type=\"application/json\" data-for=\"htmlwidget-1e860d3afe832f38c6f3\">{\"x\":{\"values\":[9.93,10.5,11.2],\"options\":{\"type\":\"line\",\"height\":20,\"width\":60},\"width\":60,\"height\":20},\"evals\":[],\"jsHooks\":[]}</script>", 
              "<span id=\"htmlwidget-2df9c177e2162ef436e1\" class=\"sparkline html-widget\"></span>\n<script type=\"application/json\" data-for=\"htmlwidget-2df9c177e2162ef436e1\">{\"x\":{\"values\":28.55,\"options\":{\"type\":\"line\",\"height\":20,\"width\":60},\"width\":60,\"height\":20},\"evals\":[],\"jsHooks\":[]}</script>", 
              "<span id=\"htmlwidget-c40cd5d7ee0b49b09243\" class=\"sparkline html-widget\"></span>\n<script type=\"application/json\" data-for=\"htmlwidget-c40cd5d7ee0b49b09243\">{\"x\":{\"values\":null,\"options\":{\"type\":\"line\",\"height\":20,\"width\":60},\"width\":60,\"height\":20},\"evals\":[],\"jsHooks\":[]}</script>", 
              "<span id=\"htmlwidget-5a14240c2eb07b4a6005\" class=\"sparkline html-widget\"></span>\n<script type=\"application/json\" data-for=\"htmlwidget-5a14240c2eb07b4a6005\">{\"x\":{\"values\":[11.29,11.32],\"options\":{\"type\":\"line\",\"height\":20,\"width\":60},\"width\":60,\"height\":20},\"evals\":[],\"jsHooks\":[]}</script>"
)

df = data.frame("Alphabet" = letters[1:5],
                Sparkline = test_moo)
out = as.htmlwidget(formattable(df))
out$dependencies = c(out$dependencies, htmlwidgets:::widget_dependencies("sparkline", "sparkline"))
out

Through trial and error, I found that putting null between square brackets, fixed the issue.

ramnathv commented 5 years ago

This is a known issue. I will push a fix so that NAs are converted to NULL during conversion to JSON.