The goal of tabletools is to provide functions which are helpful for generating summary html tables. For details on the functions, please see help documentation in the package.
Install from Github:
devtools::install_github("JMLuther/tabletools", build_vignettes = TRUE)
These functions are most useful in conjunction with an html table generating package::functions such as:
htmlTable::htmlTable
Tables with htmlTable and some alternativesknitr::kable
see Create Awesome HTML Table with knitr::kable and kableExtraformattable
This summary function is customized to my preferences, and should provide enough data to pipe into ggplot or a simple summary table. Just provides a summary for a single continuous variable.
Table:
mtcars %>%
group_by(cyl) %>%
my_summary(mpg, digits = 1) %>% # override the default 2 decimal point rounding
htmlTable::htmlTable(rnames=F, align = "c",
css.cell = rbind(rep("padding-left: .5em; padding-right: .5em;",times=ncol(.)),
matrix("padding:0 5px 0 5px;", ncol=ncol(.), nrow=nrow(.))))
cyl | mpg_n | mpg_mean | mpg_sd | mpg_sem | mpg_median | mpg_ci_025 | mpg_ci_975 |
---|---|---|---|---|---|---|---|
4 | 11 | 26.7 | 4.5 | 1.4 | 26 | 21.4 | 33.5 |
6 | 7 | 19.7 | 1.5 | 0.5 | 19.7 | 17.8 | 21.3 |
8 | 14 | 15.1 | 2.6 | 0.7 | 15.2 | 10.4 | 19 |
Plot:
mtcars %>%
group_by(cyl) %>%
my_summary(mpg) %>%
ggplot(aes(factor(cyl), mpg_mean,
ymin = mpg_ci_025, ymax = mpg_ci_975)) +
geom_linerange() +
geom_point(color = "red", size=2) +
labs(x="No. of Cylinders",
y= "Average MPG",
title= "Average MPG by number of Cylinders") +
theme_bw()
mtcars %>%
summarise(mean_sd = txt_mean_sd(mpg),
mean_sem = txt_mean_sem(mpg),
mean_range = txt_mean_range(mpg),
median_iqr = txt_median_iqr(mpg),
pct_fr = txt_pct_fr(vs, 1)
) %>%
tidyr::gather(txt_f, output) %>% # this is better than using t() which creates a matrix
htmlTable::htmlTable(rnames=F, align = "l", align.header = "l",
caption = "Example results",
css.cell = rbind(rep("padding-left: .5em; padding-right: .5em;",times=ncol(.)),
matrix("padding:0 5px 0 5px;", ncol=ncol(.), nrow=nrow(.))))
Example results | |
txt_f | output |
---|---|
mean_sd | 20.1±6.0 |
mean_sem | 20.1±1.1 |
mean_range | 20.1(10.4-33.9) |
median_iqr | 19.2\[15.4-22.8\] |
pct_fr | 43.8%(14⁄32) |
The following functions provide useful # Session Info
R version 4.2.1 (2022-06-23 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19044) Matrix products: default attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] tidyr_1.2.1 tabletools_0.1.0 htmlTable_2.4.1 ggplot2_3.3.6 [5] dplyr_1.0.10
To cite R in publications use:
R Core Team (2022). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/.
To cite the ggplot2 package in publications use:
Wickham H (2016). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. ISBN 978-3-319-24277-4, https://ggplot2.tidyverse.org.