Render datatable with sparklines in Shiny

357 views
Skip to first unread message

Grant

unread,
Jan 5, 2017, 2:54:52 AM1/5/17
to Shiny - Web Framework for R
I want to include sparklines in a shiny DT. It works fine in the RStudio viewer but in Shiny the sparklines are not rendered. Here is a minimal example. I suspect the problem has to do with adding the sparkline dependencies. It's not clear to me how to use 'spk_add_deps()' in shiny, hence my hack of adding a dummy sparkline outside of the DT to get the dependencies attached.
 
# dependencies
 
require(sparkline)
 
require(DT)
 
require(shiny)


# create data with sparklines
  spark_data
<- data.frame(
    id
= c('spark1', 'spark2'),
    spark
= c(
      spk_chr
(values = 1:3, elementId = 'spark1'),
      spk_chr
(values = 3:1, elementId = 'spark2')
   
)
 
)
 
# render in RStudio viewer (this works)
  tbl
<- datatable(spark_data, escape = FALSE)
  spk_add_deps
(tbl)


# render in Shiny (no sparklines rendered in DT)
  ui
<- fluidPage(
      sparklineOutput
("test_spark"),
      dataTableOutput
("tbl")
 
)
 
  server
<- function(input, output) {
   
# sparkline outside DT (works fine) - this also ensures sparkline dependencies are attached
      output$test_spark
<- renderSparkline(sparkline(1:3))
   
   
# sparkline inside DT (does not render)
      output$tbl
<- renderDataTable(
        expr
= spark_data,
        escape
= FALSE
     
)
 
}
 
  shinyApp
(ui = ui, server = server)



Reply all
Reply to author
Forward
0 new messages