Shiny DT:排序时冻结行名?

Jer*_*myB 6 javascript r shiny dt

我正在设计一个 Shiny 应用程序,根据各种指标对人们进行排名。使用 DT 排序功能,我希望用户能够单击任何列并对其进行排序。

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

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

我在 RStudio 帮助页面上没有看到这个选项: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)
Run Code Online (Sandbox Code Playgroud)

cor*_*ory 5

这是使用此 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)
Run Code Online (Sandbox Code Playgroud)