knitr::opts_chunk$set( collapse = TRUE, warning = FALSE, message = FALSE, comment = "#>" ) library(dplyr)
In data analysis, providing a summary of the data in the form of a table, in addition to generating a graph, is highly beneficial for expressing the results.
To generate tables, you can use a variety of R packages. kableExtra and gt are the two main packages I'd want to introduce. These packages bring a lot of options for customizing outputs. They each have their own set of strengths and limitations. Choose between them usually based on the type of output format :
gt
is best for HTML, MS Word-compatible, and also supports Latex, but not fully-featured.kableExtra
is best for Latex, Rmarkdown PDF and HTML.The stable version of packages can be installed from CRAN with:
install.packages("kableExtra") install.packages("gt")
You can also install the development version of them from GitHub:
devtools::install_github("haozhu233/kableExtra") devtools::install_github("rstudio/gt")
To learn about the gt package features and see many examples, you can see An informative document, gt-cookbook. The gt tables' output format is determined by the rmarkdown output. It generates html table codes in the case of html output and latax codes in the case of pdf output.
if (!require(gt)) install.packages('gt') library(gt) head(mtcars) %>% gt(rownames_to_stub = TRUE) %>% opt_row_striping() %>% tab_header( title = "Motor Trend Car Road Tests", subtitle = "From the 1974 Motor Trend US magazine" )
Many features of the gt
package can be customized, including:
gtcars %>% filter(ctry_origin %in% c("United States", "Japan")) %>% select(mfr:year, mpg_c, mpg_h, ctry_origin, msrp) %>% gt( rowname_col = "ctry_origin", groupname_col = "year" ) %>% summary_rows( groups = TRUE, columns = c("msrp"), fns = list(Total = ~sum(.)) ) %>% grand_summary_rows( columns = c("msrp"), fns = list(Overall = ~sum(.)) ) %>% data_color( columns = c("mpg_c", "mpg_h"), colors = scales::col_numeric( palette = c( "white", "green"), domain = c(10, 25)) ) %>% fmt_missing( columns = contains("mpg"), missing_text = "" )
The kableExtra
package is intended to enhance the basic functionality of knitr::kable
tables.
The most impressive aspect of kableExtra
is that the majority of its table capabilities are compatible with both HTML and PDF. It's a good choice of kableExtra
if you can generate a PDF (LaTex) report. For more example of use this package for PDF output see this
manual.
Below, there are some examples from kableExtra documents that are shown.
strongly recommends using the booktabs = T
option for pdf output.
In the HTML manual, this option is removed.
if (!require(kableExtra)) install.packages('kableExtra') library(kableExtra) dt <- mtcars[1:5, 1:6] dt %>% kbl() %>% kable_material(c("striped", "hover"))
mtcars[1:8, 1:8] %>% kbl() %>% kable_paper(full_width = F) %>% column_spec(2, color = spec_color(mtcars$mpg[1:8]), link = "https://haozhu233.github.io/kableExtra/") %>% column_spec(6, color = "white", background = spec_color(mtcars$drat[1:8], end = 0.7), popover = paste("am:", mtcars$am[1:8]))
mpg_list <- split(mtcars$mpg, mtcars$cyl) disp_list <- split(mtcars$disp, mtcars$cyl) inline_plot <- data.frame(cyl = c(4, 6, 8), mpg_box = "", mpg_hist = "", mpg_line1 = "", mpg_line2 = "", mpg_points1 = "", mpg_points2 = "", mpg_poly = "") inline_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)) %>% column_spec(4, image = spec_plot(mpg_list, same_lim = TRUE)) %>% column_spec(5, image = spec_plot(mpg_list, same_lim = FALSE)) %>% column_spec(6, image = spec_plot(mpg_list, type = "p")) %>% column_spec(7, image = spec_plot(mpg_list, disp_list, type = "p")) %>% column_spec(8, image = spec_plot(mpg_list, polymin = 5))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.