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)
)
)
)
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)
)
)
)
}
)
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
})
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