【问题标题】:Shiny DT: Freeze rownames while sorting?Shiny DT:在排序时冻结行名?
【发布时间】:2019-11-25 08:31:59
【问题描述】:

我正在设计一款 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)

【问题讨论】:

标签: javascript r shiny dt


【解决方案1】:

这是一个使用this SO answer的工作示例

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)

【讨论】:

  • 天才!感谢您发现它,它工作得很好。
猜你喜欢
  • 2015-10-07
  • 2021-12-18
  • 2021-12-27
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
  • 2015-09-27
  • 2021-12-04
相关资源
最近更新 更多