将DataTables插件添加到Shiny应用程序

时间:2017-12-03 21:39:47

标签: r datatables shiny dt

我想将time plugin(是的,我知道它已被弃用)添加到Shiny应用中。对于现有插件,过程似乎微不足道 - 只是copy js file to a datatables-plugins directory所以我将插件复制到DT安装目录:

tree DT/htmlwidgets/lib/datatables-plugins
DT/htmlwidgets/lib/datatables-plugins
├── natural
│   └── natural.js
├── searchHighlight
│   ├── dataTables.searchHighlight.css
│   ├── dataTables.searchHighlight.min.js
│   └── jquery.highlight.js
└── time
    └── time.js

3 directories, 5 files

但是当我尝试一个简单的应用程序时:

library(shiny)

shinyApp(
  ui = basicPage(
    DT::dataTableOutput("dt")
  ),

  server = function(input, output) {
    output$dt <- DT::renderDataTable({
      DT::datatable(
        data.frame(x = c("7:10", "10:21", "80:12")),
        plugins = 'time', options = list(
        dom = 't',
        columnDefs = list(list(type = 'time-uni', targets = 1))
      ))
    })
  }
) 

看起来插件被正确检测到(与非现有插件相比没有错误)但它不会改变排序顺序(下面显示的降序):

enter image description here

1 个答案:

答案 0 :(得分:1)

DataTables插件是客户端JavaScript,因此您必须启用客户端处理才能在Shiny中使用DT中的任何一个(使用server = FALSE呈现)。< / p>

https://rstudio.github.io/DT/plugins.html

library(shiny)

shinyApp(
  ui = basicPage(
    DT::dataTableOutput("dt")
  ),

  server = function(input, output) {
    output$dt <- DT::renderDataTable({
      DT::datatable(
        data.frame(x = c("7:10", "10:21", "80:12")),
        plugins = 'time', options = list(
          dom = 't',
          columnDefs = list(list(type = 'time-uni', targets = 1))
        ))
    }, server = FALSE)
  }
)