Using {DT} package for interactive tables

Author

Julia Romanowska

Quarto/Rmarkdown

{DT} package outputs pretty tables as .html files, using the JavaScript language to obtain interactivity. Therefore, it’s best to use them by including the code within a Quarto or RMarkdown document.

Read some data

Usually, when creating a report in Quarto/Rmarkdown, we want to summarise the entire analysis. First, we read the data and provide a short description:

Code
skimr::skim(abm)
Data summary
Name abm
Number of rows 581
Number of columns 22
_______________________
Column type frequency:
numeric 22
________________________
Group variables None

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
casenum 0 1.00 291.00 167.86 1.0 146.0 291.0 436.00 581.0 ▇▇▇▇▇
year 72 0.88 74.46 3.11 68.0 72.0 75.0 78.00 80.0 ▅▅▇▅▇
month 81 0.86 6.63 3.20 1.0 4.0 7.0 9.00 12.0 ▇▅▇▆▇
age 81 0.86 13.66 19.69 0.1 0.6 3.0 20.25 85.0 ▇▂▁▁▁
race 85 0.85 1.42 0.49 1.0 1.0 1.0 2.00 2.0 ▇▁▁▁▆
sex 81 0.86 1.44 0.50 1.0 1.0 1.0 2.00 2.0 ▇▁▁▁▆
blood_wbc 141 0.76 13.86 8.53 0.6 8.3 12.0 17.02 72.4 ▇▃▁▁▁
blood_neut_pct 146 0.75 63.30 19.64 0.0 52.0 67.0 78.00 96.0 ▁▂▅▇▆
blood_band_pct 153 0.74 6.68 10.63 0.0 0.0 2.0 8.00 66.0 ▇▁▁▁▁
blood_gluc 258 0.56 138.04 54.42 1.0 105.0 125.0 155.00 410.0 ▁▇▂▁▁
csf_gluc 129 0.78 58.32 34.03 0.0 34.0 61.0 77.00 240.0 ▅▇▁▁▁
csf_prot 249 0.57 155.72 215.46 1.0 41.0 83.0 180.00 1740.0 ▇▁▁▁▁
csf_rbc 271 0.53 1557.69 6660.79 0.0 4.0 27.5 280.25 69100.0 ▇▁▁▁▁
csf_wbc 101 0.83 2492.90 7455.42 0.0 77.0 336.5 1600.00 100000.0 ▇▁▁▁▁
csf_neut_pct 132 0.77 59.74 36.08 0.0 26.0 75.0 90.00 100.0 ▅▂▂▃▇
csf_lymph_pct 162 0.72 41.12 44.06 0.0 8.0 25.0 74.50 552.0 ▇▁▁▁▁
csf_mono_pct 165 0.72 1.20 7.97 0.0 0.0 0.0 0.00 97.0 ▇▁▁▁▁
gram 313 0.46 1.63 1.80 0.0 0.0 1.0 4.00 6.0 ▇▁▁▃▁
csf_cult 307 0.47 1.39 1.69 0.0 0.0 1.0 2.00 6.0 ▇▁▁▁▁
blood_cult 434 0.25 1.10 1.73 0.0 0.0 1.0 1.00 11.0 ▇▁▁▁▁
subset 0 1.00 1.51 0.50 1.0 1.0 2.0 2.00 2.0 ▇▁▁▁▇
abm 80 0.86 0.43 0.50 0.0 0.0 0.0 1.00 1.0 ▇▁▁▁▆

Run analyses

We want to check whether any of the variables are associated with the outcome, so we can run many univariate logistic regressions.

Code
explanatory <- names(abm)[c(-1,-22)]
dependent <- "abm"

all_glmuni_results <- finalfit::glmuni(abm, dependent, explanatory)
all_glmuni_results_tidy <- map(
  all_glmuni_results,
  ~ broom::tidy(.x, conf.int = TRUE, exp = TRUE)
) %>% 
  bind_rows() %>%
  filter(!(term %in% c("(Intercept)", "blood_cult")))

Since there are many results, we can display it in a nice DT table:

Code
datatable(
  all_glmuni_results_tidy %>%
    select(-statistic) %>%
    # make a column displaying the confidence interval nicely
    mutate(
      CI = sprintf("%.2f - %.2f", conf.low, conf.high),
      across(c(estimate, p.value, std.error), ~ signif(.x, 2))
    ) %>%
    select(term, estimate, CI, std.error, p.value),
  colnames = c("Dependent variable", "OR", "95% CI", "std error", "p-value"),
  # class = "order-column",
  class = 'cell-border stripe',
  caption = "Table 1: results of univariate GLM runs",
  filter = "top", # show filtering options on top of each column
  rownames = FALSE, # don't show row names
  options = list(
    pageLength = 5, # how many items per page?
    buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
    dom = 'Bfrtip'
  ),
  extensions = "Buttons"
)