A core design element of tfrmt
is to offer the ability
to layer tfrmts together. This ability provides an opportunity to build
template table formats that can be shared, improved, and reused across
multiple projects. tfrmt
provides a few tfrmt templates to
facilitate the creation of basic tfrmts, but they require customization.
This can be done through layering.
To layer tfrmts, you can pipe or use the layer_tfrmt()
function. Piping is the preferred method for readability.
When a tfrmt gets layered, the values in the first tfrmt are
coalesced with values in the second tfrmt. If the second tfrmt has any
of the same parameters specified as the first tfrmt, the values from the
second tfrmt are prioritized. The exception to this rule is the
“body_plan”, which will stack the plans together. To change this
behavior see the layer_tfrmt()
documentation.
Example
Here is an example of layering through piping. We create a template
from tfrmt_sigdig()
and pipe it into a separate
tfrmt
.
We provide tfrmt_sigdig()
with a data.frame detailing
the groups and the significant digits the values are to be rounded to
and it creates a tfrmt
with a body_plan that supports
this.
The second tfrmt defines the table specific information, including title, specific columns to use for row labels and output columns, as well as the col_plan.
The output tfrmt generates a table with the subset
data_labs
.
data_labs_subset <- data_labs %>%
filter(
group2 %in% c("ALANINE AMINOTRANSFERASE" ,"ALBUMIN" ,"ALKALINE PHOSPHATASE","ASPARTATE AMINOTRANSFERASE", "BASOPHILS"),
rowlbl %in% c("Bsln", "End[1]")
)
data_input <- tribble(
~group1, ~group2, ~sigdig,
"CHEMISTRY", ".default", 3,
"CHEMISTRY", "ALBUMIN", 1,
"CHEMISTRY", "CALCIUM", 1,
".default", ".default", 2
)
labs_tfrmt_template <- tfrmt_sigdig(
sigdig_df = data_input,
group = vars(group1, group2),
label = rowlbl,
param_defaults = param_set("[{n}]" = NA)
)
labs_tfrmt <- labs_tfrmt_template %>%
tfrmt(
column = vars(col1, col2),
param = param,
value = value,
sorting_cols = vars(ord1, ord2, ord3),
row_grp_plan = row_grp_plan(
label_loc = element_row_grp_loc(location = "indent")
),
col_plan = col_plan(
group1, group2,
rowlbl,
"Residuals" = res,
"Change From Baseline" = cbl,
n,
-starts_with("ord")
)
)
labs_tfrmt %>%
print_to_gt(data_labs_subset) %>%
tab_options(
container.width = 1000
)
Placebo | Xanomeline High Dose | Xanomeline Low Dose | |||||||
---|---|---|---|---|---|---|---|---|---|
Residuals | Change From Baseline | n | Residuals | Change From Baseline | n | Residuals | Change From Baseline | n | |
CHEMISTRY | |||||||||
ALANINE AMINOTRANSFERASE | |||||||||
Bsln | 17.5698 (9.21577) | [86] | 19.2024 (10.04781) | [84] | 17.9634 (8.71984) | [82] | |||
End[1] | 18.1190 (16.73781) | 0.4167 (15.39614) | [84] | 19.4750 (7.43916) | 0.0750 (8.07524) | [80] | 18.2561 (8.26387) | 0.2625 (7.24742) | [82] |
ALBUMIN | |||||||||
Bsln | 39.84 (2.807) | [86] | 40.29 (2.839) | [84] | 39.77 (2.564) | [82] | |||
End[1] | 39.63 (3.321) | -0.23 (2.690) | [84] | 39.81 (2.481) | -0.55 (2.638) | [80] | 39.23 (2.970) | -0.54 (2.643) | [82] |
ALKALINE PHOSPHATASE | |||||||||
Bsln | 77.6977 (58.10600) | [86] | 71.0482 (38.84802) | [83] | 73.3333 (20.72016) | [81] | |||
End[1] | 84.6429 (84.85858) | 7.0714 (49.36678) | [84] | 70.2500 (37.90845) | -1.6000 (12.00169) | [80] | 71.6341 (23.80295) | -1.0759 (13.08725) | [82] |
ASPARTATE AMINOTRANSFERASE | |||||||||
Bsln | 23.2442 (7.50206) | [86] | 23.1071 (6.61145) | [84] | 23.4390 (8.23737) | [82] | |||
End[1] | 25.1310 (21.32524) | 1.8214 (19.03463) | [84] | 22.7000 (6.13023) | -0.4000 (6.35968) | [80] | 23.2073 (8.20981) | -0.3000 (8.05896) | [82] |
HEMATOLOGY | |||||||||
BASOPHILS | |||||||||
Bsln | 0.054 (0.0364) | [85] | 0.052 (0.0204) | [81] | 0.054 (0.0308) | [81] | |||
End[1] | 0.048 (0.0258) | -0.007 (0.0288) | [84] | 0.051 (0.0244) | -0.001 (0.0207) | [81] | 0.049 (0.0269) | -0.006 (0.0293) | [82] |
Conflicting Layers
As two tfrmt are layered, there may come a case where the groups
value is not the same across the body plan. Additionally, data may be
provided in a slightly different group names than was expected.
Addressing this without having to re-write the entire body_plan is able
to be done with the update_group()
function.
Similar to the rename()
function, here we provide the
new group name and which old group name needs to be updated. The
function maps across the entire tfrmt and updates all references of the
old group name to the new one.
For example, this is useful when a template tfrmt needs references updated. This could happen when the input data set has a slightly different naming convention than expected.
## provided data had different column names for groups
alternate_data_labs_subset <- data_labs_subset %>%
rename(
`Lab Type` = group1,
`Lab Test` = group2,
)
labs_tfrmt %>%
update_group(
`Lab Type` = group1,
`Lab Test` = group2,
) %>%
tfrmt(
col_plan = col_plan(
`Lab Type`, `Lab Test`,
rowlbl,
"Residuals" = res,
"Change From Baseline" = cbl,
n,
-starts_with("ord")
)
) %>%
print_to_gt(alternate_data_labs_subset) %>%
tab_options(
container.width = 1000
)
Placebo | Xanomeline High Dose | Xanomeline Low Dose | |||||||
---|---|---|---|---|---|---|---|---|---|
Residuals | Change From Baseline | n | Residuals | Change From Baseline | n | Residuals | Change From Baseline | n | |
CHEMISTRY | |||||||||
ALANINE AMINOTRANSFERASE | |||||||||
Bsln | 17.5698 (9.21577) | [86] | 19.2024 (10.04781) | [84] | 17.9634 (8.71984) | [82] | |||
End[1] | 18.1190 (16.73781) | 0.4167 (15.39614) | [84] | 19.4750 (7.43916) | 0.0750 (8.07524) | [80] | 18.2561 (8.26387) | 0.2625 (7.24742) | [82] |
ALBUMIN | |||||||||
Bsln | 39.84 (2.807) | [86] | 40.29 (2.839) | [84] | 39.77 (2.564) | [82] | |||
End[1] | 39.63 (3.321) | -0.23 (2.690) | [84] | 39.81 (2.481) | -0.55 (2.638) | [80] | 39.23 (2.970) | -0.54 (2.643) | [82] |
ALKALINE PHOSPHATASE | |||||||||
Bsln | 77.6977 (58.10600) | [86] | 71.0482 (38.84802) | [83] | 73.3333 (20.72016) | [81] | |||
End[1] | 84.6429 (84.85858) | 7.0714 (49.36678) | [84] | 70.2500 (37.90845) | -1.6000 (12.00169) | [80] | 71.6341 (23.80295) | -1.0759 (13.08725) | [82] |
ASPARTATE AMINOTRANSFERASE | |||||||||
Bsln | 23.2442 (7.50206) | [86] | 23.1071 (6.61145) | [84] | 23.4390 (8.23737) | [82] | |||
End[1] | 25.1310 (21.32524) | 1.8214 (19.03463) | [84] | 22.7000 (6.13023) | -0.4000 (6.35968) | [80] | 23.2073 (8.20981) | -0.3000 (8.05896) | [82] |
HEMATOLOGY | |||||||||
BASOPHILS | |||||||||
Bsln | 0.054 (0.0364) | [85] | 0.052 (0.0204) | [81] | 0.054 (0.0308) | [81] | |||
End[1] | 0.048 (0.0258) | -0.007 (0.0288) | [84] | 0.051 (0.0244) | -0.001 (0.0207) | [81] | 0.049 (0.0269) | -0.006 (0.0293) | [82] |