【问题标题】:Descending sorting by rows index data frame Shiny按行索引数据框降序排序闪亮
【发布时间】:2016-11-03 18:52:55
【问题描述】:

我正在使用 RenderDataTable 来显示数据框。 使用order = list(list(column_number, 'desc')) 适用于除索引之外的任何列。 我想按降序排序,以便在顶部查看最新条目。使用 0 在 RenderDataTable 中不起作用。任何想法强制选项列表中的降序排序?默认总是按索引升序。

这是我的尝试:

    DT::datatable(reporting[],
                  options = list(
                    lengthMenu = c(10, 25, 50, 100, 150, 200),
                    order = list(list(0, 'desc')),
                    pageLength = 25
                  ))

【问题讨论】:

  • 为什么不订购您的reporting[] data.frame?
  • 当操作按钮等于 0 时,这实际上是在 if 语句中,否则我调用函数 reporting_update() 代替 reporting[]。这个函数从 SQLite DB 中获取数据。我将不得不对数据库的 rowid 进行排序。在显示屏上似乎更容易排序。

标签: r sorting shiny


【解决方案1】:

您可以尝试order = DT[order(as.numeric(rownames(DT)),decreasing = TRUE)],它应该按降序排列您的数据。虽然,如果没有数据,很难复制您的问题。这是我能够制作的一个工作示例:

DT = data.table(x=rep(c("b","a","c"),each=3), y=c(1,3,6), v=1:9)

DT

 x y v
1: b 1 1
2: b 3 2
3: b 6 3
4: a 1 4
5: a 3 5
6: a 6 6
7: c 1 7
8: c 3 8
9: c 6 9

DT[order(as.numeric(rownames(DT)),decreasing = TRUE)]

x y v
1: c 6 9
2: c 3 8
3: c 1 7
4: a 6 6
5: a 3 5
6: a 1 4
7: b 6 3
8: b 3 2
9: b 1 1

【讨论】:

  • 谢谢,它有效。出于某种原因,我不得不添加一个逗号DT[order(as.numeric(rownames(DT)),decreasing = TRUE), ]来解决错误undefined columns selected order
猜你喜欢
  • 1970-01-01
  • 2010-10-27
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 2018-05-26
  • 1970-01-01
  • 2021-08-10
  • 1970-01-01
相关资源
最近更新 更多