Shiny datatable() - sorting not working properly

L_ST

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)
Stéphane Laurent

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)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Primefaces DataTable not sorting properly

sorting not working in p:datatable

DataTable clearFilter() not working properly

shiny datatable paging not working in shinydashboard

Search, pagination and sorting not working in datatable

Sorting HTML table not working properly

sorting not working properly angular 4

Firebase Sorting is not working properly in flutter

Querying datatable with Linq not working properly

Links in jquery dataTable not working properly

jQuery datatable searching is not working properly

DataTable sorting does not sort properly after merging

Shiny datatable Tooltip/popover not working with paging

Sorting datatable using ultimate date plugin not working

Elasticsearch sorting not working properly based on time

Custom Field Sorting not working properly in WordPress

It seems that array sorting method is not working properly

Primefaces p:datatable sortBy not working properly

R Shiny - Disabling specific rows in a datatable with column sorting

Printing like a character but sorting like numeric in Shiny and DataTable

Bubble sorting in Java is not working properly. It shows only 0

Elasticsearch High Level Rest Client Java sorting not working properly

Sort command in not working properly in unix for sorting a csv file

VBA sort not working properly when sorting on values that are produced by formula

Sorting Access Form with unbound labels not working properly or consistently

jquery DataTable server side add Row not working properly

Yajra laravel datatable package not working properly in laravel 5.1 with mongodb

SQLite Query is not working properly - can't load datatable

DataTable Sorting - Sorting by a click on the header