install.packages("gt")
Creating Insightful Tables with gt() in R
The gt
package is used to create and customize beautiful, high-quality tables for data display and reporting. It offers a wide range of options for formatting, styling, and adding features such as headers, footers, and source notes. The gt()
function is ideal for turning raw data into polished, publication-ready tables.
Getting Started
To begin using the gt
library, start by installing it on your system:
Importing Libraries & Dataset
library(gt)
library(dplyr)
<- read.csv("C:/Users/SANKHYA/Downloads/ClinicalData.csv",check.names = FALSE)
df head(df)
PatientID Gender Age Treatment Smoking_status WeightKg VAS_Score QOL_Score
1 ID_1 Male 42 TreatmentB Smoker 57 6 79
2 ID_2 Male 69 TreatmentB Non-Smoker 95 5 59
3 ID_3 Male 39 TreatmentA Smoker 79 4 63
4 ID_4 Female 42 TreatmentB Non-Smoker 70 6 57
5 ID_5 Male 49 TreatmentB Non-Smoker 72 5 84
6 ID_6 Female 63 TreatmentB Smoker 85 7 56
Description of the dataset
This dataset provides information on patients in a clinical study, including their gender, age, treatment type, smoking status, weight, and scores for Visual Analog Scale (VAS) and Quality of Life (QOL). It aims to assess treatment outcomes and patient-reported measures related to health and well-being.
Let’s explore some functions of thegt
library:
gt_preview(): It provides a well-formatted preview of a data table.
#Preview the data
gt_preview(df)
PatientID | Gender | Age | Treatment | Smoking_status | WeightKg | VAS_Score | QOL_Score | |
---|---|---|---|---|---|---|---|---|
1 | ID_1 | Male | 42 | TreatmentB | Smoker | 57 | 6 | 79 |
2 | ID_2 | Male | 69 | TreatmentB | Non-Smoker | 95 | 5 | 59 |
3 | ID_3 | Male | 39 | TreatmentA | Smoker | 79 | 4 | 63 |
4 | ID_4 | Female | 42 | TreatmentB | Non-Smoker | 70 | 6 | 57 |
5 | ID_5 | Male | 49 | TreatmentB | Non-Smoker | 72 | 5 | 84 |
6..99 | ||||||||
100 | ID_100 | Male | 47 | TreatmentB | Non-Smoker | 68 | 5 | 85 |
tab_stubhead(): It sets the label for row headers in a table.
md(): It interprets input text as Markdown.
#Non-Smokers by Gender & Treatment
%>% filter(Smoking_status=="Non-Smoker") %>% group_by(Treatment, Gender) %>%
df summarise(Counts=n()) %>% gt(rowname_col = "Gender", groupname_col = "Treatment") %>%
tab_stubhead(label = md("**No. of Non-Smokers by Gender & Treatment**"))
No. of Non-Smokers by Gender & Treatment | Counts |
---|---|
TreatmentA | |
Female | 12 |
Male | 17 |
TreatmentB | |
Female | 13 |
Male | 16 |
tab_source_note(): It provides source attribution notes.
tab_footnote(): It adds supplementary contextual notes to a table.
#Summary Statistics by Smoking Status
%>% group_by(Smoking_status) %>% summarise(`VAS Score`=round(mean(VAS_Score),2),
df `QOL Score`=round(mean(QOL_Score),2))%>%
gt() %>% tab_header(title = "Summary Statistics by Smoking Status") %>%
tab_source_note(source_note = html("Clinical Study")) %>%
tab_footnote(footnote = md("Statistics presented is the mean"),
locations = cells_column_labels(columns=vars(`VAS Score`, `QOL Score`)))
Summary Statistics by Smoking Status | ||
Smoking_status | VAS Score1 | QOL Score1 |
---|---|---|
Non-Smoker | 6.66 | 69.88 |
Smoker | 6.50 | 73.45 |
Clinical Study | ||
1 Statistics presented is the mean |
tab_spanner(): It creates a spanning header row in tables.
#Aggregate Information by Smoking Status
%>% group_by(Smoking_status) %>%
df summarise(Treatment = names(which.max(table(Treatment))),
`VAS Score` = names(which.max(table(VAS_Score))),
`QOL Score` = names(which.max(table(QOL_Score))),
Gender = n()) %>% gt() %>%
tab_spanner(label="Aggregate Information by Smoking Status",
columns=c(Treatment,`VAS Score`, `QOL Score`, Gender)) %>%
tab_header(title = md("**Summary of Clinical Data by Smoking Status**"),
subtitle = md("*Most Frequent Treatment, VAS Score, QOL Score, and Gender*"))
Summary of Clinical Data by Smoking Status | ||||
Most Frequent Treatment, VAS Score, QOL Score, and Gender | ||||
Smoking_status | Aggregate Information by Smoking Status | |||
---|---|---|---|---|
Treatment | VAS Score | QOL Score | Gender | |
Non-Smoker | TreatmentA | 8 | 54 | 58 |
Smoker | TreatmentA | 6 | 86 | 42 |
Note:
Other functions include as_gt, fmt_number, cols_hide, gt_show, cells_title, tab_options and many more.
Features of gt
:
Customizable Tables, Markdown Support, Statistical Summaries, Grouping and Spanning, Export Options.
Conclusion:
The gt()
function in R is a powerful tool for transforming raw data into polished, publication-ready tables. Its extensive customization options allow for professional presentation and detailed reporting. Using gt
, users can effectively communicate complex data insights in a clear and engaging manner, elevating the overall quality of their data reporting and analysis.