1 knitr

knitr package includes a handy kable() function that produces simple tables:

data(mtcars)
knitr::kable( head(mtcars))
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

2 pander

pander package is a relatively simple to use package. It can produce well-formatted tables for the outputs of summary() function for the datasets and models:

library(pander)
pander( summary(mtcars[, 1:4]) )
mpg cyl disp hp
Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0
1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5
Median :19.20 Median :6.000 Median :196.3 Median :123.0
Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7
3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0
Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0
model <- lm( mpg ~ cyl, data=mtcars )    # a simple lenear regression
pander( summary(model) )  
  Estimate Std. Error t value Pr(>|t|)
(Intercept) 37.88 2.074 18.27 8.369e-18
cyl -2.876 0.3224 -8.92 6.113e-10
Fitting linear model: mpg ~ cyl
Observations Residual Std. Error \(R^2\) Adjusted \(R^2\)
32 3.206 0.7262 0.7171

3 DT

DT package can display matrices and data frames as tables and provides filtering and sorting. It is especially useful to display long tables that do not fit on a single page:

DT::datatable(data=mtcars)

4 kableExtra

kableExtra package extends functionality of the kable package.

library(kableExtra)
df <- mtcars[1:5, 1:6]

df %>%
  kbl() 
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440

This package has a few themes:

df %>%
  kbl() %>%
  kable_minimal()
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440

4.1 kable_styling

Function kable_styling() allows other ways to customize the output table:

df %>%
  kbl() %>%
  kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440

kable_styling() includes the following options:

  • full_width - TRUE or FALSE (default - TRUE)
  • position - “left”, “center”, “right”, “float_left”, “float_right”
  • font_size - a numeric value
  • fixed_thead - T or F (option to fix the header row on top)

4.2 Column_spec

This function can be used to format specific columns, e.g:

df %>%
  kbl() %>%
  kable_minimal() %>%
  column_spec(1, bold = T) %>%
  column_spec(2, background="lightyellow")
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440

We can also use a conditional formatting for the values in a column based on a value of another variable:

df %>%
  kbl() %>%
  kable_minimal() %>%
  column_spec(1, bold = T) %>%
  column_spec(5, color=ifelse(df$hp==110, "red", "black"))
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440

4.3 Row_spec

Similar to column_spec, we can use row_spec() function to format rows:

df %>%
  kbl() %>%
  kable_paper(full_width = F) %>%
  column_spec(1, bold = T) %>%
  row_spec(3, background="brown", color="white", bold = TRUE)
mpg cyl disp hp drat wt
Mazda RX4 21.0 6 160 110 3.90 2.620
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875
Datsun 710 22.8 4 108 93 3.85 2.320
Hornet 4 Drive 21.4 6 258 110 3.08 3.215
Hornet Sportabout 18.7 8 360 175 3.15 3.440

4.4 Sparklines

We can include inline plots into this table. The following types of plots are available:

  • spec_hist
  • spec_boxplot
  • spec_plot

First let’s prepare the data:

(mpg_list <- split(mtcars$mpg, mtcars$cyl))
## $`4`
##  [1] 22.8 24.4 22.8 32.4 30.4 33.9 21.5 27.3 26.0 30.4 21.4
## 
## $`6`
## [1] 21.0 21.0 21.4 18.1 19.2 17.8 19.7
## 
## $`8`
##  [1] 18.7 14.3 16.4 17.3 15.2 10.4 10.4 14.7 15.5 15.2 13.3 19.2 15.8 15.0

Then we need to define a table with the desired number of rows and columns and give columns names:

df_plot <- data.frame(cyl = c(4, 6, 8), 
                      mpg_box = "", 
                      mpg_hist = "",
                      min = unname(sapply(mpg_list, min)), 
                      max = unname(sapply(mpg_list, max)),
                      mean = unname(sapply(mpg_list, mean)))

Now we can add the plots into the first two columns:

df_plot  %>%
  kbl(booktabs = TRUE) %>%
  kable_paper(full_width = FALSE) %>%
  column_spec(2, image = spec_boxplot(mpg_list)) %>%
  column_spec(3, image = spec_hist(mpg_list)) 
cyl mpg_box mpg_hist min max mean
4 21.4 33.9 26.66364
6 17.8 21.4 19.74286
8 10.4 19.2 15.10000

For more examples see Create Awesome HTML Table with knitr::kable and kableExtra