-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.qmd
70 lines (66 loc) · 3.39 KB
/
index.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
---
title: "Table Drawing Research"
subtitle: "Table engine output experimentation"
engine: knitr
editor:
markdown:
wrap: 72
---
# Overview
Tables built in {gtsummary} typically use {gt} functions to draw and render
tables in different output formats. As a result, not all table customization
translated correctly. This repo is used to experiment and identify potential
adaptions to {gt} functions to maintain as much of the table decoration as possible
when exporting to other formats. Currently exploring: HTML, PDF
(with and without Typst), and Word (.docx) outputs.
Below is the sample {gtsummary} table with notable customizations (footnotes,
headers, titles, linebreaks, indentations, etc.) we are tracking.
```{r}
#| echo: false
#| message: false
library(gtsummary)
tbl <- gtsummary::tbl_summary(gtsummary::trial, by = trt, include = c(age, grade), missing = "always", label = list(age = "Age \n\U00A0\U00A0linebreak in a cell")) |> add_overall()
tbl$table_body |>
gt::gt(caption = gt::md("This is the caption \n[Used for cross referencing in Quarto; but I don't think a footnote can be attached]")) |>
gt::fmt_markdown(columns = label) |> # need this to recognize the line break in the table body
gt::cols_hide(columns = c(variable, var_type, row_type, var_label)) |>
gt::sub_missing(missing_text = "") |>
# indentation
gt::text_transform(
locations = gt::cells_body(columns = label, rows = !row_type %in% "label"),
fn = function(x) paste0(strrep("\U00A0", times = 2), x)
) |>
gt::text_transform(
locations = gt::cells_body(columns = label, rows = row_type %in% "missing"),
fn = function(x) paste0(strrep("\U00A0", times = 4), x)
) |>
# Bold cells in table
gt::tab_style(
style = gt::cell_text(weight = "bold"),
locations = gt::cells_body(columns = "label", rows = c(1, 3))
) |>
# Column headers with line breaks
gt::cols_label(
label = gt::md("**Characteristic** \n\U00A0\U00A0\U00A0\U00A0Indented Header"),
stat_0 = gt::md("**Overall**"),
stat_1 = gt::md("**Drug A** \nManual Line Break"),
stat_2 = gt::md("**Drug B** \nManual Line Break")
) |>
# column alignment
gt::cols_align(columns = c(stat_1, stat_2), align = "right") |>
# spanning headers 1 and 2 levels
gt::tab_spanner(gt::md("**Spanning Level 2**"), columns = gtsummary::all_stat_cols(), level = 2L, id = "stat_1_L2") |>
gt::tab_spanner(gt::md("**Spanning Level 1**"), columns = gtsummary::all_stat_cols(FALSE), level = 1L, id = "stat_1_L1") |>
gt::tab_spanner(gt::md("**Both Treatments**"), columns = stat_0, level = 1L, id = "stat_0_L1") |>
# footnotes and source notes
gt::tab_footnote("Footnote in Header", locations = gt::cells_column_labels(columns = label)) |>
gt::tab_footnote("Footnote in Body", locations = gt::cells_body(columns = label, row = 1L)) |>
gt::tab_footnote("Footnote in Spanning Header", locations = gt::cells_column_spanners(spanners = "stat_1_L1")) |>
gt::tab_footnote("Footnote in Subtitle", locations = gt::cells_title("subtitle")) |>
gt::tab_footnote("Footnote in Title", locations = gt::cells_title("title")) |>
gt::tab_source_note("This is Source Note 1") |>
gt::tab_source_note("This is Source Note 2") |>
gt::tab_source_note("Abbreviations: Q1 = First Quartile; Q3 = Third Quartile") |>
# title, subtitle, and captions
gt::tab_header(gt::md("This is the Title \nwith a linebreak"), subtitle = "This is the Subtitle")
```