Tracy
Tracy

Reputation: 719

R shiny Datatable column not sorting as numeric

I am having trouble with DT::rednerDataTable. When my datatable is produced I have three (of 10) columns of numbers that are not getting sorted. This is what it looks like sorted: enter image description here

Here is my code:

library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardBody(
    tabItems((tabName = "ResultsTable",
              fluidPage(column(11, DT::dataTableOutput("table",width = "100%"),offset = 0))))

shinyServer(function(input, output, session) {
        output$table <- DT::renderDataTable(DT::datatable({
          data <- rv$data
          if (input$sour != "All") {
            data <- data[data[,1] == input$sour,]
          }else{data}
          if (input$sour1 != "All") {
            data <-data[data[,2] == input$sour1,]
          }else{data}
          if (input$tran2 != "All") {
            data <-data[data[,3] == input$tran2,]
          }else{data}
        }))
})

The variable data is a data.frame and the numeric columns are already sorted, but as I click the up and down arrows next to the column name in the table ( as shown below) the sorting gets mixed up.

I'd appreciate any help!

Thank you

Upvotes: 1

Views: 1819

Answers (1)

Tracy
Tracy

Reputation: 719

My data was a matrix. I first had to convert it to a data frame and then change the columns needed to numeric (some of columns strings). I didn't have to change my code above. It was in the script that produced the data table. The table is 10 columns with column's 5:8 as numeric.

 table_output=as.data.frame(table_output);
data=cbind(table_output[,1:4],as.numeric(as.character(table_output[,5])),as.numeric(as.character(table_output[,6])),as.numeric(as.character(table_output[,7])), as.numeric(as.character(table_output[,8])), table_output[,9:10])

Upvotes: 1

Related Questions