7 Tables
7.1 Displaying tables - htmlTable
htmlTable is a package that will produce the HTML code needed to display a table in a browser.
library(htmlTable)
htmlTable::htmlTable(esoph[1:5,]) # You can only see what the table looks like when Knitr generates a document| agegp | alcgp | tobgp | ncases | ncontrols | |
|---|---|---|---|---|---|
| 1 | 25-34 | 0-39g/day | 0-9g/day | 0 | 40 |
| 2 | 25-34 | 0-39g/day | 10-19 | 0 | 10 |
| 3 | 25-34 | 0-39g/day | 20-29 | 0 | 6 |
| 4 | 25-34 | 0-39g/day | 30+ | 0 | 5 |
| 5 | 25-34 | 40-79 | 0-9g/day | 0 | 27 |
There are various options to change how a htmlTable will look:
htmlTable::htmlTable(esoph[1:5,],
rnames = FALSE,
header = c("Age Group","Alcohol Group","Tobacco Group","ncases","ncontrol"), # Change headings - specify all columns
caption = "Oesophageal Cancer", #To add a Title
align = 'lllrr', #To align column values 'r'=right, 'l'=left, c='centre(default)
align.header='lllcc', #To align column headers
css.table = "width: 70%; table-layout: fixed; word-wrap: break-word;") # Add CSS formats to the table| Oesophageal Cancer | ||||
| Age Group | Alcohol Group | Tobacco Group | ncases | ncontrol |
|---|---|---|---|---|
| 25-34 | 0-39g/day | 0-9g/day | 0 | 40 |
| 25-34 | 0-39g/day | 10-19 | 0 | 10 |
| 25-34 | 0-39g/day | 20-29 | 0 | 6 |
| 25-34 | 0-39g/day | 30+ | 0 | 5 |
| 25-34 | 40-79 | 0-9g/day | 0 | 27 |
More css styling is avilable : https://www.w3schools.com/css/css_table.asp
7.2 Displaying Interactive tables - DT
DT is a package that will produce an interactive table in a browser.
## Warning: package 'DT' was built under R version 3.4.4
There are various options and additional format functions to change how a DT table looks:
mtcars[,1:5] %>% mutate(cyl = as.factor(cyl)) %>%
DT::datatable(colnames = c("Miles per gallon","Cylinders","disp","hp","drat") # Rename columns
,rownames = FALSE #To remove row numbers
,width = '80%'
,filter = 'top' #Add filters above each column
,options = list(pageLength = 4, # Set no. rows on each page
dom = 'rtBp')) %>% # Specify wheter search box etc. appears
DT::formatStyle(0, target = 'row', lineHeight='8px') %>% #Changes row height
DT::formatRound(columns=c('mpg', 'drat'), digits=1) #Rounds values to 1dpFor more detail : https://rstudio.github.io/DT/
7.3 Creating Tabulations
Before displaying a table you may need to coerce your data into the right shape. A basic frequency table can be created by using dplyr functions as then displayed with htmlTable:
| Cylinders | Gears | n |
|---|---|---|
| 4 | 3 | 1 |
| 4 | 4 | 8 |
| 4 | 5 | 2 |
| 6 | 3 | 2 |
| 6 | 4 | 4 |
| 6 | 5 | 1 |
| 8 | 3 | 12 |
| 8 | 5 | 2 |
When grouping by multiple categories the results will be in a tidy format.
To produce the same reuslts as a 2-way tables, you can use the spread function
mtcars %>% count(Cylinders=cyl, Gears=gear) %>%
spread(Gears, n) %>% # The 'Gears' category will be along columns, 'n' values will fill the cells
htmlTable::htmlTable(rnames=FALSE)| Cylinders | 3 | 4 | 5 |
|---|---|---|---|
| 4 | 1 | 8 | 2 |
| 6 | 2 | 4 | 1 |
| 8 | 12 | 2 |
To easily add a row totals use the adorn functions from the janitor package:
mtcars %>% count(Cylinders=cyl, Gears=gear) %>%
spread(Gears, n) %>%
janitor::adorn_totals("row") %>%
htmlTable::htmlTable(rnames=FALSE, total=TRUE)| Cylinders | 3 | 4 | 5 |
|---|---|---|---|
| 4 | 1 | 8 | 2 |
| 6 | 2 | 4 | 1 |
| 8 | 12 | 2 | |
| Total | 15 | 12 | 5 |
They only way to nest multiple categories along the column dimension, is to create a combined field using the unite function.
mtcars %>% count(Cylinders=cyl, Gears=gear, am) %>%
unite(gears_am, Gears, am, sep = " <br> ") %>%
spread(gears_am, n) %>%
htmlTable::htmlTable(rnames=FALSE)| Cylinders |
3 0 |
4 0 |
4 1 |
5 1 |
|---|---|---|---|---|
| 4 | 1 | 2 | 6 | 2 |
| 6 | 2 | 2 | 2 | 1 |
| 8 | 12 | 2 |
An alternative to the above is to reshape data with a cast function. The best version of this function is from the data.table package (it originally comes from the reshape package).
data.table::data.table(mtcars) %>%
data.table::dcast(cyl + am ~ gear, sep = "<br>", fun.aggregate = c(length, median), value.var = c("mpg","wt")) %>%
htmlTable::htmlTable(rnames=FALSE)| cyl | am |
mpg length 3 |
mpg length 4 |
mpg length 5 |
wt length 3 |
wt length 4 |
wt length 5 |
mpg median 3 |
mpg median 4 |
mpg median 5 |
wt median 3 |
wt median 4 |
wt median 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 4 | 0 | 1 | 2 | 0 | 1 | 2 | 0 | 21.5 | 23.6 | 2.465 | 3.17 | ||
| 4 | 1 | 0 | 6 | 2 | 0 | 6 | 2 | 28.85 | 28.2 | 2.0675 | 1.8265 | ||
| 6 | 0 | 2 | 2 | 0 | 2 | 2 | 0 | 19.75 | 18.5 | 3.3375 | 3.44 | ||
| 6 | 1 | 0 | 2 | 1 | 0 | 2 | 1 | 21 | 19.7 | 2.7475 | 2.77 | ||
| 8 | 0 | 12 | 0 | 0 | 12 | 0 | 0 | 15.2 | 3.81 | ||||
| 8 | 1 | 0 | 0 | 2 | 0 | 0 | 2 | 15.4 | 3.37 |
# Your data must be converted to a data.table format.
# The shape of the table is defined by a formula (eg. x1 + x2 ~ y) where variables on the left of the ~ represent row dimensions and those on the right represent column dimensions.
# You can calcuate one or more statistics (mean, max, median..) to appear in the cells, for one or more values. The function automatically aggregates (ie. no group-by is needed).