【问题标题】:Natural sorting in Shiny DT (datatables) doesn't workShiny DT(数据表)中的自然排序不起作用
【发布时间】:2015-09-27 21:43:16
【问题描述】:

亲爱的 Shiny 和 DT 大师们! 我正在尝试在我闪亮的应用程序中使用自然排序插件,但它似乎不起作用。我认为它与以前版本的 Shiny 或/和之前的 DT 包一起使用。有谁能够帮我?请参阅下面的示例(我正在尝试对最后一列进行排序):

服务器.R

library(shiny)
require(DT)
shinyServer(function(input, output) {
    output$example <- DT::renderDataTable({
        table = cbind(LETTERS[1:5],matrix(1:20,nrow=5),c(1,2,3,10,"a"))
        table = rbind(c("filtered",round(rnorm(5),3)),table)
        DT::datatable(table,
                      rownames = FALSE,
                      extensions = list(FixedColumns = list(leftColumns = 1)),
                      options = list(
                          columnDefs = list(list(type = "natural", targets = "_all"))))
    })
})

ui.R

library(shiny)
require(DT)
shinyUI(
    fluidPage(
        tags$head(
            tags$script(src = "http://cdn.datatables.net/1.10.6/js/jquery.dataTables.min.js", type = "text/javascript"),
            tags$script(src = "http://cdn.datatables.net/plug-ins/1.10.7/sorting/natural.js", type = "text/javascript")
        ),
        DT::dataTableOutput('example')
    )
)

【问题讨论】:

  • 看起来您的初始表加载不正确。
  • 抱歉,输入错误,但思路还是一样...
  • 对所有列进行排序对我来说非常有效。是否出现任何错误或警告?

标签: r datatables shiny dt


【解决方案1】:

DT (>= 0.1.16) 的current development version 中,您可以使用datatable(..., plugins = 'natural') 启用此插件,例如

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    DT::dataTableOutput('example')
  ),
  server = function(input, output) {
    output$example <- DT::renderDataTable({
      table = cbind(LETTERS[1:5],matrix(1:20,nrow=5),c(1,2,3,10,"a"))
      table = rbind(c("filtered",round(rnorm(5),3)),table)
      table
    }, server = FALSE, plugins = 'natural', options = list(
      columnDefs = list(list(type = "natural", targets = "_all"))
    ))
  }
)

更多信息请参见the documentation

【讨论】:

  • 谢谢,你们真的进步了!但是,在我们的服务器上,我们正在处理非常大的表(当然我们使用的是Scroller),所以我们不想使用server = FALSE。但它不适用于= TRUE
  • @user1991825 不幸的是,这个插件不是为服务器端处理模式设计的。为server = TRUE 实现自然排序可能有点麻烦,但无论如何您都可以向github.com/rstudio/DT/issues 提出功能请求。当我有更多时间时,我会看看是否有可能。
猜你喜欢
  • 2019-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 2017-01-01
  • 2018-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多