gpt4 book ai didi

r - 如何在 DT 的左侧有一个像 Excel 中那样的行号?

转载 作者:行者123 更新时间:2023-12-04 11:40:16 25 4
gpt4 key购买 nike

我想在左边有一个始终显示 1, 2, 3, ... 的行号(就像 excel 行号一样),这样用户就有一个引用行号来查看单击列以更改表格顺序后感兴趣的行的排名。

编辑:

library(DT)
n <- 20
set.seed(1)
df <- data.frame(customer_id = sample(10),
amount_2016 = round(runif(n) * 16),
amount_2017 = round(runif(n) * 17),
amount_2018 = round(runif(n) * 18),
amount_2019 = round(runif(n) * 19))

datatable(df)

The default

点击amount_2016后,显示

order by amount_2016

它显示 17, 14, 1, 2, 15, ... 我希望它一直是 1, 2, 3, ... 就像在 Excel 中

最佳答案

诀窍是定义一个callback JS() 函数来实现table.on behaviour一个动态行号计数器。

这是一个最小的可重现 RMarkdown 示例:

---
title: "Untitled"
output: html_document
---

```{r}
library(DT)
datatable(
iris,
callback = JS("
table.on( 'order.dt search.dt', function () {
table.column(0, {search:'applied', order:'applied'}).nodes().each(
function (cell, i) {
cell.innerHTML = i+1;
} );
} )"))
```

生产

enter image description here

无论您如何排列列,行号始终保持为 1、2、3、...

关于r - 如何在 DT 的左侧有一个像 Excel 中那样的行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54360602/

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