htmlwidgets / sparkline

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

Sparklines only appear on first page of DT table #26

Closed smach closed 6 years ago

smach commented 6 years ago

In following the example code from this stackoverflow question: https://stackoverflow.com/questions/47041415/include-sparkline-htmlwidget-in-datatable-cells-in-a-shiny-app-without-resortin

library(shiny)
library(DT)
library(data.table)
library(sparkline)

my_mtcars <- data.table(mtcars, keep.rownames = TRUE)
names(my_mtcars)[1] <- 'car_id'
set.seed(0)
data_for_sparklines <- data.table(car_id = rep(my_mtcars$car_id, 5),
                                  category = 1:5,
                                  value = runif(160))
sparkline_html <- data_for_sparklines[, .(sparkbar = spk_chr(value, type = 'bar')), by = 'car_id']
my_mtcars <- merge(my_mtcars, sparkline_html, by = 'car_id')

The sparklines show up on the first page of the data table but not subsequent pages. Do you know how to get the sparklines to appear on all pages of the table?

timelyportfolio commented 6 years ago

I have not done this in a while and am off computer but I think you just need fnDrawCallback as shown in https://github.com/htmlwidgets/sparkline/issues/22.

timelyportfolio commented 6 years ago

For a longer discussion https://github.com/rstudio/DT/issues/410 might be helpful.

smach commented 6 years ago

Yes that's it exactly. Adding

options = list(
  fnDrawCallback = htmlwidgets::JS(
    '
function(){
  HTMLWidgets.staticRender();
}
'
  )

to my datatable solves the problem. I'll make myself an RStudio code snippet to remember and will be good to go in the future. Thanks very much!!

timelyportfolio commented 6 years ago

@smach, great - I will close. Feel free to reopen.