knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)
library(tidyverse)
library(htmlTable)
library(tabletools)

tabletools

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.

Installation

Install from Github: devtools::install_github("JMLuther/tabletools")

These functions are most useful in conjunction with an html table generating package::functions such as:

my_summary custom summary

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(.))))

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()

Summary text functions

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

Session Info

Hmisc::markupSpecs$html$session()


JMLuther/tabletools documentation built on July 1, 2024, 2:01 p.m.