title: Include sparkline htmlwidget in datatable cells in a Shiny app, without resorting to (much) JavaScript
body: <p>I am using the <code>sparkline</code> package to produce bar charts to place into cells of a <code>datatable</code> in a Shiny app. I've managed to produce the desired output in a standalone <code>datatable</code>, but when I place it into the Shiny app it doesn't work. It may have something to do with how <code>spk_add_deps()</code> identifies the htmlwidgets. I've tried moving the <code>spk_add_deps()</code> function around quite a bit and passing it various identifiers, and nothing worked.</p>
  
  <p>I did find essentially the same question here <a href="https://stackoverflow.com/questions/41477776/render-datatable-with-sparklines-in-shiny">Render datatable with sparklines in Shiny</a>
  but the given solution (1) relies on writing the JavaScript code for the sparklines in a callback function (defeating the purpose of having the R <code>sparkline()</code> function) and (2) it seems that if the sparklines render in the viewer then we can't be all that far off from getting them to render in the Shiny app without having to write all that JavaScript.</p>
  
  <p>Here's the demo:</p>
  
  <pre><code># Preliminary, load packages and build a demo table with the sparkline code merged in
  library(shiny)
  library(DT)
  library(data.table)
  library(sparkline)
  
  ## Create demo data sets
  my_mtcars &lt;- data.table(mtcars, keep.rownames = TRUE)
  names(my_mtcars)[1] &lt;- 'car_id'
  
  set.seed(0)
  data_for_sparklines &lt;- data.table(car_id = rep(my_mtcars$car_id, 5),
                                    category = 1:5,
                                    value = runif(160))
  
  sparkline_html &lt;- data_for_sparklines[, .(sparkbar = spk_chr(value, type = 'bar')), by = 'car_id']
  my_mtcars &lt;- merge(my_mtcars, sparkline_html, by = 'car_id')
  </code></pre>
  
  <p>Now if I render the datatable on its own the sparkline bar graphs do appear:</p>
  
  <pre><code>spk_add_deps(datatable(my_mtcars, escape = FALSE))
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/di8wz.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/di8wz.jpg" alt="datatable showing sparkline bar charts"></a></p>
  
  <p>But if I embed the same into a Shiny app that column is blank:</p>
  
  <pre><code>ui &lt;- shinyUI(fluidPage(
    dataTableOutput('myTable')
  ))
  
  server &lt;- shinyServer(function(input, output, session) {
    output$myTable &lt;- renderDataTable(spk_add_deps(datatable(my_mtcars, escape = FALSE)))
  }) 
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/BNkiX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BNkiX.png" alt="enter image description here"></a></p>