noblabla
noblabla

Reputation: 113

Descending sorting by rows index data frame Shiny

I'm using RenderDataTable to display data frame. Using order = list(list(column_number, 'desc')) works for any column except the index. I would like to sort by descending order in order to see the newest entries at the top. Using 0 doesn't work in RenderDataTable. Any idea to force the descending sorting in options list? The default is always ascending order by index.

Here is my attempt:

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

Upvotes: 0

Views: 1873

Answers (1)

josh453
josh453

Reputation: 318

You could try order = DT[order(as.numeric(rownames(DT)),decreasing = TRUE)] which should order your data in descending order. Although, it is hard to replicate your problem without the data. Here is a working example I was able to produce:

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

Upvotes: 0

Related Questions