library(dplyr) library(epitable) library(knitr) knitr::opts_chunk$set( cache = FALSE )
color: black; border-bottom: 1px black; border-top: 2px black;
r Sys.Date()
epitable
produces tables in the style of epidemiological papers. The tables are intended for knitr Rmarkdown documents. The look of the tables depend on the CSS of the Rmarkdown document. This vignette was created using Rstudios default CSS. The package has two functions:
freq_by()
Creates percentage and frequencies and prints to HTML. Input is a dataset
and a character vector of column names (var_vector
) in the input dataset. A
by_groupis a string referring to a column in the
dataset` for which to stratify the calculations. model_to_html()
Creates HTML presentation of statistical models. Takes a list of univariate models, and a list of multivariate models which contain sequentially more covariates. freq_by
The freq_by
-function creates a frequency and percentage table commonly used in epidemiological papers. It is a wrapper for the excellent htmlTables-package.
head(example_data)
The by_group
variable must be of class factor, to ensure correct order. The argument min_cell_count
sets the minimum number of observations in a cell. It is inteded to preserve anonymity in case of sensitive data. When the cell count is <= min_cell_count
, the number is deleted from that cell.
freq_by(dataset = example_data, var_vector = c("color", "clarity"), by_group = "cut" )
model_to_html
The model_to_html
-function can print simple models to HTML, also printing the reference levels of the categorical covariates. It does not support variables of class ordered
.
The example below tages two lists of models. One list of univariate models, and another list of multivariate models. The multivariate models include incrementally more variables, so that the final multivariate model include all the variables used in the univariate models.
# univariate models in a list: c("Age" , "Embarked" , "Sex" , "Fare" , "Pclass") %>% paste0( "Survived ~ ", . ) %>% purrr::map( ~ glm( as.formula(.), data = titanic, family = "binomial" )) -> univar_list # multivariate models in a list: glm_logistic_1 <- glm( Survived ~ Age + Embarked, data = titanic, family = "binomial") glm_logistic_2 <- glm( Survived ~ Age + Embarked + Sex + Fare, data = titanic, family = "binomial") glm_logistic_3 <- glm( Survived ~ Age + Embarked + Sex + Fare + Pclass, data = titanic, family = "binomial") multi_model_list <- list(glm_logistic_1, glm_logistic_2, glm_logistic_3 )
Using the two lists of models defined above the table is created:
model_to_html( univar_list, multi_model_list, exponentiate = TRUE )
The example below creates a list of univariate models of class coxph
, and a list of multivariate models of class coxph
. The output is a table showing the how the parameters change as more are added sequentially.
lung <- survival::lung c( "inst", "age", "sex", "ph.ecog", "ph.karno", "pat.karno", "meal.cal", "wt.loss" ) %>% paste0( " survival::Surv( time, status==2) ~ ", . ) %>% purrr::map( ~ survival::coxph( as.formula(.), data = lung )) -> univariate_coxph_list coxph_model1 <- survival::coxph( survival::Surv( time, status==2) ~ inst + age + sex, lung) coxph_model2 <- survival::coxph( survival::Surv( time, status==2) ~ inst + age + sex + ph.ecog + ph.karno, lung) coxph_model3 <- survival::coxph( survival::Surv( time, status==2) ~ inst + age + sex + ph.ecog + ph.karno + pat.karno, lung) coxph_model4 <- survival::coxph( survival::Surv( time, status==2) ~ inst + age + sex + ph.ecog + ph.karno + pat.karno + meal.cal + wt.loss, lung) multivariate_coxph_list <- list( coxph_model1, coxph_model2, coxph_model3, coxph_model4 ) model_to_html( univariate_coxph_list, multivariate_models_list = multivariate_coxph_list )
You can change the number of digits and the font:
model_to_html( univariate_coxph_list, multivariate_models_list = multivariate_coxph_list, font_css = "font-family: Times;", decimals_estimate = 4)
It is possible to change the font using the font_css
argument, but the font needs to be web-friendly. Below the font is set to "font-family: Courier":
freq_by(dataset = example_data, var_vector = c("color", "clarity"), by_group = "cut", font_css = "font-family: Arial" )
by_group
sfreq_by
-function.The default CSS of Rstudio looks great, but sometimes the content part is too narrow, which can introduce linebreaks in a wide table. To avoid this you can increase the width of the content. To do this put this code at the top of the rmarkdown document (but below the yaml). It sets the content width to 1400 pixels.
<style type="text/css"> .main-container { max-width: 1400px; margin-left: auto; margin-right: auto; } </style>
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.