2
votes

The row names of the iris dataframe are "1", "2", "3", ... When I set the 0-th column as orderable with DT, the ordering behaves as if the row names were numeric:

library(DT)
datatable(iris, 
          options = list(
            columnDefs = list(
              list(orderable=TRUE, targets=0)
            )
          )
) 

enter image description here

Nice. Now, when I do the same inside shiny, the behaviour is different: the ordering behaves as if the row names were character strings:

library(shiny)
shinyApp(
  ui = fluidPage(fluidRow(column(12, DTOutput('tbl')))),
  server = function(input, output) {
    output$tbl = renderDT(
      iris, options = list(
        columnDefs = list(
          list(orderable=TRUE, targets=0)
        )
      )
    )
  }
)

enter image description here

Not nice. What is the cause of the difference? I'd like to have the first behavior in Shiny. As a workaround, we could set a numeric column at the first position and set rownames=FALSE, but I'm wondering whether there's an easier solution and I'm intrigued by this difference.

EDIT

I've finally proceed in this way:

output$tbl = renderDT({
  dt <- datatable(
    iris, options = list(
      columnDefs = list(
        list(orderable=TRUE, targets=0)
      )
    )
  )
  dt$x$data[[1]] <- as.numeric(dt$x$data[[1]]) 
  dt
})
1
not sure if it matches your requirement, but you could turn off the server side processing: renderDT(..., server = FALSE). It would be easier as it avoids the funny behaviour on the server side, but still a workaround i guess ;)Tonio Liebrand

1 Answers

0
votes

SO is telling me I need 50 reputation to comment, so here's my comment in answer form.

Another workaround would be to do the following:

output$tbl = renderDT({
  dt <- datatable(
    iris %>%
       rownames_to_column("UID") %>%
       select(UID, everything()),
 options = list(
      columnDefs = list(
        list(orderable=TRUE, targets=0)
      )
    )
  )
  dt

It doesn't answer your question of why it happens though.