Skip to content
Table one for Latex and html markdown documents
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R
man
.Rbuildignore
.gitignore
DESCRIPTION
KableOne.Rproj
KableOne_LaTex_Demo-concordance.tex
KableOne_LaTex_Demo.Rnw
KableOne_LaTex_Demo.log
KableOne_LaTex_Demo.pdf
KableOne_LaTex_Demo.synctex.gz
KableOne_LaTex_Demo.tex
NAMESPACE
README.Rmd
README.md

README.md

KableOne

The goal of KableOne is to make it easy for analysts to include a Table 1 object in both LaTeX and html markdown documents. I made this package because I was unable to get the html tables I wanted from the existing table one R packages. Notably, this package has far fewer features than the outstanding TableOne package, but it can get a readable Table 1 object into a markdown document with less effort.

Installation

You can install the latest version of KableOne from github with:

devtools::install_github('bcjaeger/KableOne')

Example

This example shows the basic elements of KableOne. First, we apply some aesthetic changes to the data:

library(labelled)
#> Warning: package 'labelled' was built under R version 3.5.3
library(tidyverse)
#> -- Attaching packages ------------------------------------------------------ tidyverse 1.2.1 --
#> v tibble  2.0.1       v purrr   0.3.1  
#> v tidyr   0.8.3       v dplyr   0.8.0.1
#> v readr   1.3.1       v stringr 1.4.0  
#> v tibble  2.0.1       v forcats 0.4.0
#> Warning: package 'tidyr' was built under R version 3.5.3
#> Warning: package 'purrr' was built under R version 3.5.3
#> -- Conflicts --------------------------------------------------------- tidyverse_conflicts() --
#> x dplyr::filter() masks stats::filter()
#> x dplyr::lag()    masks stats::lag()
library(survival)
library(kableExtra)
#> 
#> Attaching package: 'kableExtra'
#> The following object is masked from 'package:dplyr':
#> 
#>     group_rows
library(KableOne)

data = pbc %>%
  dplyr::select(
    age, sex, status, trt, stage, ascites, bili, edema, albumin
  ) %>%
  dplyr::mutate(
    status=factor(
      status, levels = c(0:2),
      labels = c("Censored", "Transplant", "Dead")
    ),
    stage = factor(
      stage, levels = c(1:4),
      labels = c("One", "Two", "Three", "Four")
    ),
    trt=factor(
      trt, levels=c(1:2),
      labels = c("Drug A", "Drug B")
    ),
    ascites=factor(
      ascites, levels=c(0:1),
      labels = c("No", "Yes")
    ),
    sex = fct_recode(
      sex,
      'Male'='m',
      'Female'='f'
    ),
    edema = factor(
      edema,
      levels=c(0, 0.5, 1),
      labels=c("None", "A little", "Lots")
    )
  ) 

Next, we label the columns of the data. These labels will be passed along when our table is made.

data = data %>%
  set_variable_labels(
    status = "Status at last contact",
    trt = "Treatment group",
    age = 'Age, years',
    sex = 'Sex at birth',
    ascites = 'Ascites',
    bili = 'Bilirubin levels, mg/dl',
    edema = 'Is there Edema?'
  ) %>%
  set_variable_groups(
    Outcomes = c('status'),
    Exposures = c('ascites','bili','edema','trt','albumin','stage')
  ) 

Now we can apply the tibble_one function to get an object that we can pass along to an Rmarkdown document.

tbl_one = data %>%
  tibble_one(
    strat='trt',
    include.allcats = TRUE,
    include.freq = FALSE,
    include.pval = TRUE
  )

Last step, we pass tbl_one into the kibble_one function, which provides a couple of nice formatting procedures to make the data look like the type of Table 1 that you may see in a published article.

kibble_one(
  tbl_one,
  use.groups = TRUE
) %>%
  kable_styling(
    position = 'center',
    bootstrap_options = c('striped')
  ) %>% 
  footnote(
    general = 'Table values are mean(sd) and percent for continuous and categorical variables, respectively'
  )
Treatment group
Overall Drug A Drug B P-value
No. of observations 418 158 154
Age, years 50.7 (10.4) 51.4 (11.0) 48.6 (9.96) 0.018
Sex at birth 0.421
Male 10.5 13.3 9.74
Female 89.5 86.7 90.3
Exposures
Stage 0.201
One 5.10 7.59 2.60
Two 22.3 22.2 20.8
Three 37.6 35.4 41.6
Four 35.0 34.8 35.1
Ascites 0.567
No 92.3 91.1 93.5
Yes 7.69 8.86 6.49
Bilirubin levels, mg/dl 3.22 (4.41) 2.87 (3.63) 3.65 (5.28) 0.133
Is there Edema? 0.877
None 84.7 83.5 85.1
A little 10.5 10.1 8.44
Lots 4.78 6.33 6.49
Albumin 3.50 (0.42) 3.52 (0.44) 3.52 (0.40) 0.874
Outcomes
Status at last contact 0.894
Censored 55.5 52.5 55.2
Transplant 5.98 6.33 5.84
Dead 38.5 41.1 39.0
Note:
Table values are mean(sd) and percent for continuous and categorical variables, respectively
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.