闪亮的DT:在排序时冻结行名吗? [英] Shiny DT: Freeze rownames while sorting?

查看:56
本文介绍了闪亮的DT:在排序时冻结行名吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个Shiny应用,用于根据各种手段对人们进行排名。我希望使用DT排序功能,用户可以单击任何列并按其排序。

I'm designing a Shiny app to rank people based on a variety of metics. Using the DT sorting feature, I want users to be able to click on any column and sort by it.

使用行名作为排名似乎很自然;问题是这些数字与表格的其余部分一起排序。有什么方法可以冻结此列,以便在表的其余部分排序时,排名数字保持不变?也许具有JavaScript函数?

It seems natural to use the rownames as the rank; the problem is that these numbers sort along with the rest of the table. Is there any way to freeze this column so the rank numbers stay the same while the rest of the table sorts? Perhaps with a JavaScript function?

编辑:在下面的示例中,当我单击 Metric_1时,我希望行名保持为1、2、3、4,而不是排序为3、2 ,1、4以匹配人员C,人员B,人员A,人员D的新顺序。结束编辑

In the example below, when I click "Metric_1," I want the rownames to stay 1, 2, 3, 4, instead of sorting to 3, 2, 1, 4 to match the new order of Person C, Person B, Person A, Person D. END EDIT

在RStudio帮助中看不到此选项页面: https://rstudio.github.io/DT/

I don't see this option on the RStudio help page: https://rstudio.github.io/DT/

# Simplified example

library(shiny)
library(DT)


ui <- fluidPage(

    DT::dataTableOutput("table")

)

server <- function(input, output) {

    output$table <- DT::renderDataTable({

        x <- data.frame(
            Name = c("Person A", "Person B", "Person C", "Person D"), 
            Metric_1 = c(8, 7, 4, 10), 
            Metric_2 = c(3, 5, 2, 8)
        )

        datatable(x)

    })
}

shinyApp(ui = ui, server = server)


推荐答案

下面是使用此SO答案

library(shiny)
library(DT)


ui <- fluidPage(

  DT::dataTableOutput("table")

)

server <- function(input, output) {
  js <- c(
    "table.on('draw.dt', function(){",
    "  var PageInfo = table.page.info();",
    "  table.column(0, {page: 'current'}).nodes().each(function(cell,i){", 
    "    cell.innerHTML = i + 1 + PageInfo.start;",
    "  });",
    "})")

  output$table <- DT::renderDataTable({

    x <- data.frame(
      Name = c("Person A", "Person B", "Person C", "Person D"), 
      Metric_1 = c(8, 7, 4, 10), 
      Metric_2 = c(3, 5, 2, 8)
    )

    datatable(x, callback = JS(js))

  })
}

shinyApp(ui = ui, server = server)

这篇关于闪亮的DT:在排序时冻结行名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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