cbailiss/basictabler

Does basictabler support multiple columns as row headers

Closed this issue · 1 comments

Does it support multiple columns (eg. column 1 to 3) to be row headers?

Sorry for the very slow reply.

This is possible using the current version of the package using the numberOfColumnsAsRowHeaders argument, e.g. with the qtbl() function or with tbl$addData().

Example:

# aggregate the sample data to make a small data frame
library(basictabler)
library(dplyr)
tocsummary <- bhmsummary %>%
  group_by(TOC, PowerType) %>%
  summarise(OnTimeArrivals=sum(OnTimeArrivals),
            OnTimeDepartures=sum(OnTimeDepartures),
            TotalTrains=sum(TrainCount)) %>%
  ungroup() %>%
  mutate(OnTimeArrivalPercent=OnTimeArrivals/TotalTrains*100,
         OnTimeDeparturePercent=OnTimeDepartures/TotalTrains*100) %>%
  arrange(TOC)

# To specify formatting, a list is created which contains one element for each column in 
# the data frame, i.e. tocsummary contains six columns so the columnFormats list has six elements.
# The values in the first column in the data frame won't be formatted since NULL has been specified.
# The values in the 2nd, 3rd and 4th columns will be formatted using format(value, big.mark=",")
# The values in the 5th and 6th columns will be formatted using sprintf(value, "%.1f")
columnFormats=list(NULL, NULL, list(big.mark=","), list(big.mark=","), list(big.mark=","), "%.1f", "%.1f")

# render the table directly as a html widget
qhtbl(tocsummary, firstColumnAsRowHeaders=TRUE, numberOfColumnsAsRowHeaders=2,
      explicitColumnHeaders=c("TOC", "PowerType", "On-Time Arrivals", "On-Time Departures",
                              "Total Trains", "On-Time Arrival %", "On-Time Departure %"),
      columnFormats=columnFormats)

This can also be done after a table has been created using the tbl$setStyling() function.

This was also possible in earlier versions of the package but needed more code to achieve, so I suggest updating to the current version of the package as it is a lot easier now.