knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" ) library(dplyr) library(ggplot2) library(htmlTable) library(tabletools) library(tidyr)
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 alternatives knitr::kable
see Create Awesome HTML Table with knitr::kable and kableExtra formattable
my_summary
custom summaryThis 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(.))))
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(.))))
The following functions provide useful
Hmisc::markupSpecs$html$session()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.