rpivottabler

pivottabler: formating numbers with comma


I wonder how to display numbers with comma using qpvt and qhpvt functions from pivottabler R package.

library(pivottabler)
qpvt(bhmtrains, "TOC", "TrainCategory", "n()")
                    Express Passenger  Ordinary Passenger  Total  
Arriva Trains Wales               3079                 830   3909  
CrossCountry                     22865                  63  22928  
London Midland                   14487               33792  48279  
Virgin Trains                     8594                       8594  
Total                            49025               34685  83710  

qhpvt(bhmtrains, "TOC", "TrainCategory", "n()")

Solution

  • As the functions are evaluated as expressions as mentioned in the vignette

    ... Specify the calculation. The summarise expression must be an expression that can be used with the dplyr summarise() function. This expression is used internally by the pivottabler package with the dplyr summarise function.

    we can wrap those with comma from formattable

    library(pivottabler)
    qhpvt(bhmtrains, "TOC", "TrainCategory", "formattable::comma(n(), digits = 0)")
    

    -output

    enter image description here


    There is also a format/formats argument on which we can either a single format or a list of formats (when multiple expressions are used) i.e. if we want to show the digits

    qhpvt(bhmtrains, "TOC", "TrainCategory", "n()", format = "%.2f" )
    

    enter image description here