Home > other >  Shiny datatable() - sorting not working properly
Shiny datatable() - sorting not working properly

Time:10-04

I am trying to create a Shiny App that displays a datatable. So far, the Shiny App is working fine, except for the problem that the created data table does not sort properly when it has to deal with a different number of digits before the decimal point. However, the sorting works within a fixed number of digits before the decimal point. Attached is a screenshot to better understand my problem.

So far, I have tried to find a solution by multiplying the numbers by 1000 and adding a decimal point in the datatable function, but the problem persists.

Below is a simplified version of the dashboard code.

Since this is my first Stackoverflow post, I'm trying my best to provide all the information needed, and I appreciate any responses. Please let me know if any information is missing or if the problem is not clearly described.

Thanks a lot in advance!

Example Picture

# Library ======================================================================

library(dplyr)
library(shinythemes)
library(DT)
library(shiny)


### Pre-settings ===============================================================

input_choice <- c("X1","X2") 

Y_names <- c("Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8","Y9","Y10","Y11","Y12","Y13","Y14","Y15")
X1 <- c(22.27613,NA,22.56672,21.12475,22.26027,NA,24.7519,NA,4.4752,7.5555,12.7221,9.9999,10,10.0001,10.001)
X2 <- c(15.06880,17.37105,18.93447,17.39056,17.40290,13.00607,22.2901,5.8915,6.7728,8.5642,10.0012,99.9999,100,999.9999,1000)
Year <- c(rep(2019,15))

data_ranking_2019 <- as.data.frame(cbind(Y_names,X1,X2,Year))


### UI function ================================================================

ui <- navbarPage(

    # Application title
    "Shiny App",
    theme = shinytheme("flatly"),
    
    
    # Country Ranking Tab Panel =============
    
    tabPanel("Country Ranking",
      fluidRow(
        
        # Data Table output
        column(11, offset = 1, textOutput("tab_name_rankings"),
              
              # Data table
              wellPanel(
                dataTableOutput("rank_table_top"))
              )
        )
      )
)



### Server function ============================================================



server <- function(input, output){
  
  ### Create data set depending on input$years ###
  data_ranking <- reactive({    
    data <- data_ranking_2019
    data %>%
      select(Y_names, all_of(input_choice))
  })

  ### Create data table ###
  output$rank_table_top <- DT::renderDT({
    
    # Data Processing
    data <- as.data.frame(data_ranking())
    
    # Create Data Table
    data_table <- data %>% DT::datatable(
      selection = 'none', 
      
      # Customization
      options = list(paging = F,
                     scrollX = "100%"))

    # Return data table
    return(data_table)
  }) 

}

# Run the application 
shinyApp(ui = ui, server = server)

CodePudding user response:

That's because when you use cbind, this creates a character matrix because there is a character column. A matrix cannot have some mixed types. Instead, do:

data_ranking_2019 <- data.frame(Y_names, X1, X2, Year)
  • Related