Number arranging as per the units

I am trying to add the units to numbers in the dataframe. But I see after formatting, sorting is not working as expected (since the column is now characters). I need to sort as per numbers only (Millions coming at last). But this is not happening

library(shiny)
library(DT)

ui <- fluidPage(
  DTOutput("tab")
)

server <- function(input, output, session) {
  
  format_numbers <- function (df, column_name){
    df[[column_name]] <- ifelse(nchar(df[[column_name]]) <= 5, paste(format(round(df[[column_name]] / 1e3, 1), trim = TRUE), "K"),
                                paste(format(round(df[[column_name]] / 1e6, 1), trim = TRUE), "M"))
  }
  
  df <- data.frame(x = c(12345,35666,2646575,345))
  
  df$x <- format_numbers(df, "x")
  
  output$tab <- renderDT({
    datatable(df,escape = F)
  })
  
}

shinyApp(ui, server)

This is similar to the question you posted a few months ago. The solution I provided then should help out in this scenario too. In short, sort the formatted column using the numeric source column, then hide it in the DT.

1 Like

Perfect got it. Thanks. But I did a small change .
The change is, I created a function for DT table and then called it under render function. Looks like some issue here.

Basically same concept only but calling it under function. Can you please help me here

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

ui <- fluidPage(
  DTOutput("tab")
    
)

server <- function(input, output, session) {
  
  table1 <- mtcars
  table1$new <- rownames(mtcars)
  table1 <- table1 %>% mutate(sort_new = ifelse(new == "Hornet 4 Drive",1,ifelse(new == "Mazda RX4 Wag",2, ifelse(new == "Mazda RX4",3,
                                                                                                                  ifelse(new == "Hornet Sportabout",4,
                                                                                                                         ifelse(new == "Valiant",5,
                                                                                                                                ifelse(new == "Datsun 710",6,7))))))) %>% arrange(desc(-sort_new))
  
  
  dt_table <- function(data, for_ord, ord_for){
    DT::datatable(data, 
                  options = list(
                    columnDefs = list(
                      list(orderData=for_ord,targets=ord_for),
                      list(visible=FALSE,targets=for_ord)
                    )
                  )
    )
  }
  
  
  output$tab <- renderDT({
    dt_table(head(table1, n = 6), for_ord = "sort_new", ord_for = "new")
  })
}

shinyApp(ui, server)

But below code is working (Just changed column names by column number. Not sure what the is reason behind this.

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

ui <- fluidPage(
  DTOutput("tab")
    
)

server <- function(input, output, session) {
  
  table1 <- mtcars
  table1$new <- rownames(mtcars)
  table1 <- table1 %>% mutate(sort_new = ifelse(new == "Hornet 4 Drive",1,ifelse(new == "Mazda RX4 Wag",2, ifelse(new == "Mazda RX4",3,
                                                                                                                  ifelse(new == "Hornet Sportabout",4,
                                                                                                                         ifelse(new == "Valiant",5,
                                                                                                                                ifelse(new == "Datsun 710",6,7))))))) %>% arrange(desc(-sort_new))
  
  
  dt_table <- function(data, for_ord, ord_for){
    DT::datatable(data, 
                  options = list(
                    columnDefs = list(
                      list(orderData=for_ord,targets=ord_for),
                      list(visible=FALSE,targets=for_ord)
                    )
                  )
    )
  }
  
  
  output$tab <- renderDT({
    #dt_table(head(table1, n = 6), for_ord = "sort_new", ord_for = "new")
    dt_table(head(table1, n = 6), for_ord = 13, ord_for = 12)  ## Change here
  })
}

shinyApp(ui, server)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.