在Shiny应用程序的数据表单元格中包含迷你图htmlwidget,而无需使用(太多)JavaScript [英] Include sparkline htmlwidget in datatable cells in a Shiny app, without resorting to (much) JavaScript

查看:176
本文介绍了在Shiny应用程序的数据表单元格中包含迷你图htmlwidget,而无需使用(太多)JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用sparkline程序包生成条形图,以放置到Shiny应用程序中datatable的单元格中.我已经设法在独立的datatable中产生所需的输出,但是当我将其放入Shiny应用程序中时,它将无法正常工作.它可能与spk_add_deps()如何标识htmlwidgets有关.我试着将spk_add_deps()函数移了很多,并给它传递了各种标识符,但没有任何效果.

I am using the sparkline package to produce bar charts to place into cells of a datatable in a Shiny app. I've managed to produce the desired output in a standalone datatable, but when I place it into the Shiny app it doesn't work. It may have something to do with how spk_add_deps() identifies the htmlwidgets. I've tried moving the spk_add_deps() function around quite a bit and passing it various identifiers, and nothing worked.

我确实在这里找到了基本相同的问题在Shiny中使用迷你图渲染数据表 但是给定的解决方案(1)依赖于在回调函数中编写迷你图的JavaScript代码(违背了具有R sparkline()函数的目的)和(2)看来,如果迷你图在查看器中呈现,则我们可以在无需编写所有JavaScript的情况下,让它们在Shiny应用程序中呈现并不是一件遥不可及的事情.

I did find essentially the same question here Render datatable with sparklines in Shiny 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 sparkline() 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.

这是演示:

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

现在,如果我自己渲染数据表,则出现迷你图条形图:

Now if I render the datatable on its own the sparkline bar graphs do appear:

spk_add_deps(datatable(my_mtcars, escape = FALSE))

但是,如果我将其嵌入到Shiny应用程序中,则该列为空白:

But if I embed the same into a Shiny app that column is blank:

ui <- shinyUI(fluidPage(
  dataTableOutput('myTable')
))

server <- shinyServer(function(input, output, session) {
  output$myTable <- renderDataTable(spk_add_deps(datatable(my_mtcars, escape = FALSE)))
}) 

shinyApp(ui = ui, server = server)

推荐答案

使用htmlwidgets包找到了解决方案.

Found a solution, using the htmlwidgets package.

library(htmlwidgets)

然后使用getDependency()代替spk_add_deps()在Shiny UI函数中加载迷你图依赖项:

Then instead of spk_add_deps() use getDependency() to load the sparkline dependencies in the Shiny UI function:

ui <- shinyUI(fluidPage(
  getDependency('sparkline'),
  dataTableOutput('myTable')
))

由于我不完全了解的原因,请在renderDataTable()中向HTMLwidgets staticRender()函数添加回调:

And for reasons I don't fully understand, add a callback in renderDataTable() to the HTMLwidgets staticRender() function:

server <- shinyServer(function(input, output, session) {
  staticRender_cb <- JS('function(){debugger;HTMLWidgets.staticRender();}') 
  output$myTable <- renderDataTable(my_mtcars,
                                    escape = FALSE,
                                    options = list(drawCallback = staticRender_cb))
}) 

仅此而已,这就是使它们在Shiny应用程序中呈现的全部方法.

But that's it, that's all it takes to get them to render in a Shiny app.

这篇关于在Shiny应用程序的数据表单元格中包含迷你图htmlwidget,而无需使用(太多)JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆