【问题标题】:Render datatable with sparklines in Shiny在 Shiny 中使用迷你图渲染数据表
【发布时间】:2017-05-19 13:37:45
【问题描述】:

我想在闪亮的 DT 中包含迷你图。它在 RStudio 查看器中运行良好,但在 Shiny 中未渲染迷你图。这是一个最小的例子。

# 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) - 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)

【问题讨论】:

  • 请参阅此link 以获取闪亮数据表中的迷你图。
  • 谢谢,是的,我确实在那个页面上花了一些时间。它涵盖了我没有问题的第一种情况(即在 RStudio Viewer 中渲染)。该链接不涵盖第二种情况(为什么它无法在 Shiny 中呈现)。我怀疑问题可能出在依赖项上。我不清楚如何在闪亮的应用程序中使用“spk_add_deps”,因此我在 DT 之外渲染了一个虚拟迷你图以附加依赖项。
  • 我在这里stackoverflow.com/questions/47041415/… 重新发布了这个问题的一个版本,因为该解决方案似乎不需要手动输入 JavaScript。如果它在查看器中有效,为什么在 Shiny 中无效?

标签: r shiny dt sparklines


【解决方案1】:

老问题,我知道,但根据问题Add label to sparkline plot in datatable 中的信息,我认为解决方案是您最初尝试的方法加上几行。在这里,我删除了在查看器中演示它的部分,并添加了使迷你图正常工作所需的部分。

# 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')
  )
)

###  adding this <------------
cb <- htmlwidgets::JS('function(){debugger;HTMLWidgets.staticRender();}')

ui <- fluidPage(
  ###  and this <------------
  htmlwidgets::getDependency('sparkline'),
  dataTableOutput("tbl")
)

server <- function(input, output) {

  output$tbl <- renderDataTable(
    expr = spark_data,
    escape = FALSE,
    ###  and this <------------
    options = list(
      drawCallback =  cb
    )
  )
}

shinyApp(ui = ui, server = server)

【讨论】:

【解决方案2】:

我已修改您的代码以生成迷你图。我参考了这个link 来生成迷你图。

require(sparkline)
require(DT)
require(shiny)

# create data
spark_data1<- data.frame(id = c('spark1', 'spark2'),
                          spark = c("1,2,3", "3,2,1"))



ui <- fluidPage(
  sparklineOutput("test_spark"),
  DT::dataTableOutput("tbl")
)

server <- function(input, output) {

  line_string <- "type: 'line', lineColor: 'black', fillColor: '#ccc', highlightLineColor: 'orange', highlightSpotColor: 'orange'"

  cd <- list(list(targets = 1, render = JS("function(data, type, full){ return '<span class=sparkSamples>' + data + '</span>' }")))

  cb = JS(paste0("function (oSettings, json) {\n  $('.sparkSamples:not(:has(canvas))').sparkline('html', { ", 
                 line_string, " });\n}"), collapse = "")

  output$tbl <- DT::renderDataTable({
    dt <-  DT::datatable(as.data.frame(spark_data1),  rownames = FALSE, options = list(columnDefs = cd,fnDrawCallback = cb))
  })
}

shinyApp(ui = ui, server = server)

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2011-02-21
    • 2021-01-17
    • 2020-11-08
    相关资源
    最近更新 更多