1
votes

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

1
Try making it a complete example. But you probably just need to convert the types of the columns to numeric with as.numeric(col) or - if they were a factor - (as.numeric(as.character(col))Mike Wise
I thought I did that already, but that was the problem. I was trying to change it while it was a matrix. I switched it to a data frame and was able to have a portion of the columns as numeric. Thank you @ Mike WiseTracy
It is actually possible that this would help someone in the future, so I will submit it as an answer if you would accept it. Is that okay? There is reputation in it for you too obviously.Mike Wise
Sure. You can post the answer @ Mike WiseTracy

1 Answers

1
votes

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