htmlwidgets / sparkline

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

Sparklines fail to render in tableHTML #35

Open ccuts opened 2 years ago

ccuts commented 2 years ago

Hi there - I posted this on StackOverflow, not sure if it's better posed to sparkline devs or tableHTML devs but it seems like the missing link is in these lines:

 formattable::formattable() %>%
  formattable::as.htmlwidget() %>%
  sparkline::spk_add_deps()

Stackoverflow question

Title is fairly self-explanatory. Here's an MWE:

summary_table <- iris %>% 
  dplyr::group_by(Species) %>%
  dplyr::summarize(
    `Petal.Length.Spark` = sparkline::spk_chr(`Petal.Length`, 
                                            type = 'line',
                                            chartRangeMin=0,
                                            chartRangeMax=10),

  `Petal.Length` = sum(`Petal.Length`)) 

tableHTML::tableHTML(summary_table, rownames=FALSE, 
                     round=2,  
                     escape = FALSE)

My hope was that adding the escape = F would render the sparklines, but it renders nothing. Everything looks right if you hit 'inspect element' -- eg, the tag for row 1/col 2 is:

<td id="tableHTML_column_2"><span id="htmlwidget-9a537f436338a4cd349d" class="sparkline html-widget"></span>
<script type="application/json" data-for="htmlwidget-9a537f436338a4cd349d">{"x":{"values":[4.7,4.5,4.9,4,4.6,4.5,4.7,3.3,4.6,3.9,3.5,4.2,4,4.7,3.6,4.4,4.5,4.1,4.5,3.9,4.8,4,4.9,4.7,4.3,4.4,4.8,5,4.5,3.5,3.8,3.7,3.9,5.1,4.5,4.5,4.7,4.4,4.1,4,4.4,4.6,4,3.3,4.2,4.2,4.2,4.3,3,4.1],"options":{"type":"line","chartRangeMin":0,"chartRangeMax":10,"height":20,"width":60},"width":60,"height":20},"evals":[],"jsHooks":[]}</script></td>

which looks right to me. Not totally sure what look at next here - it feels like this is missing the tableHTML equivalent of these lines:

formattable::formattable() %>%
  formattable::as.htmlwidget() %>%
  sparkline::spk_add_deps()

from the sparklinedocumentation forsparkline::spk_chr (replicated below, see here: https://cran.r-project.org/web/packages/sparkline/sparkline.pdf)

library(dplyr)
library(sparkline)
library(formattable)
mtcars %>%
  group_by(cyl) %>%
  summarise(
    hp = spk_chr(
      hp, type="box",
      chartRangeMin=0, chartRangeMax=max(mtcars$hp)
    ),
    mpg = spk_chr(
      mpg, type="box",
      chartRangeMin=0, chartRangeMax=max(mtcars$mpg)
    )
  ) %>%
  formattable() %>%
  formattable::as.htmlwidget() %>%
  spk_add_deps()

any insight appreciated. Would really prefer to keep this in tableHTML due to other dependencies in this project.