[edited for clarity]
I'm looking for examples or ideas on how I might display a data table in a .Rmd document that has grouped totals at multiple levels. So, in the example following, the Net Sales, Units, and AUR have a total at the district level and company level.
Following I just crafted an example to illustrate what I mean:
The screenshot illustrates what I might display to the user and below was my R attempt to get a similar result minus the formating.
library(tidyverse) library(xtable) # Create dummy data salesData <- data.frame(District=c(1,1,1,2,2,2), Name=c("S1","S2","S3","S4","S5","S6"), NetSales=c(1000,1500,3000,2000,1500,3000), Units=c(50,120,52,12,64,52)) %>% mutate(AUR = NetSales / Units) # Generates District Totals salesTotals <- salesData %>% group_by(District) %>% summarize(Name = "Total", NetSales = sum(NetSales), Units = sum(Units), AUR = sum(NetSales) / sum(Units) ) # Generates Grand Total grandTotals <- salesTotals %>% summarize( District = "", Name = "GrandTotal", NetSales = sum(NetSales), Units = sum(Units), AUR = round((sum(NetSales)/sum(Units),2))) # Binds district totals to frame totals <- rbind(salesData, salesTotals) %>% arrange(District, Name) #Binds grand total to frame totals <- rbind(totals, grandTotals) #Generate LaTeX table to display xtable(totals)
Which generates something like this using
This creates a rather rough table and with some more table formatting in
xtable, the table would look presentable.
- Is there a tidy verse way of doing this?
- How you might go about displaying this data to your users?
- Have you had to deal with this sort of display before in other projects?