Building Table 1
Learning Goals
- Append tables by rows together
tbl_stack. - Explore the structure of a
gtsummaryobject. - Customise the way to display the table headers using
modify_header. - Introduce various ways to customise footnotes.
- Show how the table can be exported in html and Word.
Using tbl_stack
In the previous section, we have completed creating our three tables as shown below.
Click on the different tabs to see the tables
We use gtsummary::tbl_stack to append the three tables together.
Understanding gtsummary object
We can now work on customising the table itself. To do this, we need to understand more about the gtsummary object.
Every gtsummary object is a list comprising of, at minimum table_body, table_styling. Here is how you can extract table_body and table_styling and print the header_names.
Click on the different tabs.
table_body object is the data frame that defines what gets printed as the output. The compulsory columns are variable, row_type and label. label depicts what is printed at the Characteristic column. stat_1 depicts what is printed at the Control column. stat_2 depicts what is printed at Treatment column. p.value is printed at the p-value column.
table_styling objects is a list of data frames containing information about how table_body is printed, formatted and styled.
gtsummary::show_header_names() prints the header/columns= names as text to the console.
Modify Header
After combining the tables, we first update the header using gtsummary::modify_header.
As we use the by parameter in gtsummary::tbl_summary, the following fields are available the stratifying columns: {level}, {n}, and {p}.
To match Table 1, we bold and add brackets to N = 410 and N = 413 in the column stat_1 and stat_2. In addition, we update p-value to P value in the column p.value.
Next Steps
The next few steps is to add footnotes for the title as well as some rows in the tables. However, the way to do this varies based on what kind of output (html or word document) you wish the table to be represented as. gtsummary has its limitations in this and need the help of other R packages such as gt and flextable. Below are examples on how this can be done.
Export in html
We proceed to export the table as html by using the R package gt.
Add footnotes in table
Footnotes in the table can be added using gtsummary::modify_footnote_body.
To match Table 1, we add the relevant footnotes at the relevant rows in the table_body data frame.
Add Title with footnotes
gtsummary does not have a function to add footnote at headers. You must proceed to convert the gtsummary object to gt and continue the work from there.
Save as html
Export in word
We proceed to export the table as html by using the R package flextable.
Add footnotes in table
Footnotes in the table are this time added using gtsummary::modify_table_body as though we can add footnote in flextable, they are always represented as superscript.
Add Title with footnotes
We proceed to convert the gtsummary object to flextable and continue the work from there.
Save as word
We save it into word using the code below. However, the code will not work as we cannot export files in this interactive session.
table1_word |>
flextable::save_as_docx(path = "data/table1.docx")