Control number formatting in Shiny's implementation of DataTable

后端 未结 3 1729
灰色年华
灰色年华 2021-01-04 03:35

Aim

Render large numbers as with thousands separators in a shiny DataTable by default.

Problem

The documentation reads (to me) like it should alr

相关标签:
3条回答
  • 2021-01-04 04:05

    Try formatting the df you pass to renderDatatable() with thousands formatting

    df$x <- format(df$x, big.mark=',', scientific=FALSE) 
    

    Or

    df$x<-prettyNum(df$x, big.mark = ",")
    
    0 讨论(0)
  • 2021-01-04 04:07

    I know this is quite an old post, however was struggling with this today too so thought that someone else may get value from this solution.

    Since the question was asked the DT package was released by RStudio (Github or CRAN). At first glance it isn't too obvious how to format numbers as their isn't a corresponding formatNumber function like their is formatCurrency. The way around it is to use the formatCurrency and just set the currency parameter to nothing. Example below.

    library(shiny)
    library(DT)
    runApp(
      list(ui = fluidPage(
        DT::dataTableOutput("mytable")
      ),
      server = function(input, output, session) {
        output$mytable <- DT::renderDataTable(
          DT::datatable(iris*1000, 
                        options = list(pageLength = 50, 
                                       columnDefs = list(list(className = 'dt-left', 
                                                              targets = 0:4)))) %>% 
            formatCurrency(1:4, '')
        )
      }
      )
    )
    
    0 讨论(0)
  • 2021-01-04 04:12

    Problem with format an prettyNum is the fact that these functions produce characters instead of numerics. Thus, it is then impossible to style your rendered table. A solution may be the following with this example :

      output$tab <- renderDataTable({ 
      datatable(mytable) %>%
          formatCurrency(columns = ..., currency = "", interval = 3, mark = ",") %>%
          formatStyle(
          columns = ...,
          color = styleInterval(myThreshold, c("black", "red"))) }) 
    
    0 讨论(0)
提交回复
热议问题