gpt4 book ai didi

javascript - Shiny 的 DT : Freeze rownames while sorting?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:53:14 24 4
gpt4 key购买 nike

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

最佳答案

这是一个使用 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)

关于javascript - Shiny 的 DT : Freeze rownames while sorting?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57060061/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com