knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(tfrmt) library(dplyr) library(gt) library(tidyr)
In tfrmt
footnotes are a table component which can be added to the
specification in a manner similar to body_plan
and col_plan
. It has its own
dedicated function called footnote_plan
which consists of a list of
footnote_structure
s.
Some options for the marks used to indicate footnotes are the following:
Let's go through some examples!
Here is some mock data we're going to use.
# Create mock data df <- crossing(group = c("group 1", "group 2"), label = c("label 1", "label 2"), column = c("PL", "T1", "T2", "T1&T2"), param = c("count", "percent")) # This one is used for examples 5 and 6 span_df <- df %>% mutate(span = case_when(column == "PL" ~ "Placebo", column %in% c("T1", "T2", "T1&T2") == TRUE ~ "Treatment"))
Different footnotes are separated by individual footnote_structure
s.
# Create specification tfrmt( group = group, label = label, column = column, param = param, row_grp_plan = row_grp_plan( row_grp_structure(group_val = ".default", element_block(post_space = " ")) ), body_plan = body_plan( frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{count} ({percent})", count = frmt("xx"), percent = frmt("xx.x"))) ), # Add footnotes here footnote_plan = footnote_plan( footnote_structure(footnote_text = "Source Note", group_val = "group 1"), footnote_structure(footnote_text = "Placebo", column_val = "PL"), marks = "standard"), ) %>% print_mock_gt(df)
When applying a footnote to multiple columns you must enter a named list where the LHS is the name of the column variable in your input data ("column" in this example) containing column header names for the output table, and the RHS is the column header name you have selected ("T1", "T2" or "T1&T2" in this case).
# Create specification tfrmt( group = group, label = label, column = column, param = param, row_grp_plan = row_grp_plan( row_grp_structure(group_val = ".default", element_block(post_space = " ")) ), body_plan = body_plan( frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{count} ({percent})", count = frmt("xx"), percent = frmt("xx.x"))) ), # Add footnotes here footnote_plan = footnote_plan( footnote_structure(footnote_text = "All Treatments", list(column = c("T1","T2", "T1&T2"))), marks = "numbers"), ) %>% print_mock_gt(df)
# Create specification tfrmt( group = group, label = label, column = column, param = param, row_grp_plan = row_grp_plan( row_grp_structure(group_val = ".default", element_block(post_space = " ")) ), body_plan = body_plan( frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{count} ({percent})", count = frmt("xx"), percent = frmt("xx.x"))) ), # Add footnotes here footnote_plan = footnote_plan( footnote_structure(footnote_text = "Footnote goes here", label_val = "label 1"), marks = "standard"), ) %>% print_mock_gt(df)
# Create specification tfrmt( group = group, label = label, column = column, param = param, row_grp_plan = row_grp_plan( row_grp_structure(group_val = ".default", element_block(post_space = " ")) ), body_plan = body_plan( frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{count} ({percent})", count = frmt("xx"), percent = frmt("xx.x"))) ), # Add footnotes here footnote_plan = footnote_plan( footnote_structure(footnote_text = "Footnote goes here", group_val = "group 1", label_val = "label 1"), marks = "standard"), ) %>% print_mock_gt(df)
# Add specification tfrmt( group = group, label = label, column = c("span", "column"), param = param, row_grp_plan = row_grp_plan( row_grp_structure(group_val = ".default", element_block(post_space = " ")) ), body_plan = body_plan( frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{count} ({percent})", count = frmt("xx"), percent = frmt("xx.x"))) ), col_plan = col_plan( group, label, span_structure(span = c("Placebo"), column = c("PL")), span_structure(span = c("Treatment"), column = c("T1", "T2", "T1&T2")) ), # Add footnote here footnote_plan = footnote_plan( footnote_structure(footnote_text = "Footnote goes here", column_val = list(span = "Treatment")), marks = "extended" ) ) %>% print_mock_gt(span_df)
You can apply a mark to multiple headers by entering a named list for your column variable.
# Add specification tfrmt( group = group, label = label, column = c("span", "column"), param = param, row_grp_plan = row_grp_plan( row_grp_structure(group_val = ".default", element_block(post_space = " ")) ), body_plan = body_plan( frmt_structure(group_val = ".default", label_val = ".default", frmt_combine("{count} ({percent})", count = frmt("xx"), percent = frmt("xx.x"))) ), col_plan = col_plan( group, label, span_structure(span = c("Placebo"), column = c("PL")), span_structure(span = c("Treatment"), column = c("T1", "T2", "T1&T2")) ), # Add footnote here footnote_plan = footnote_plan( footnote_structure(footnote_text = "Footnote goes here", column_val = list(span = "Treatment", column = "T1&T2")), marks = "extended" ) ) %>% print_mock_gt(span_df)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.