Converts internal codes into human-readable labels
Examples:
active 60min → “Active kidney center 60 min radius”
HIV 50mile → “Active HIV R+ kidney center 50 mile radius”
This ensures consistent, manuscript-friendly labeling.
Step 3: Convert Results to gt Tables
The helper function convert_resultsdf_to_table():
Orders rows consistently
Formats percentages
Applies table titles
Structures the output by year
Two tables are created per organ:
Table 1: Access to transplant in PLWH
Table 2: Access to transplant in people without HIV
Step 4: Export and Cache
Each table is:
Saved as .docx (for manuscript submission workflows)
Saved as .png (for presentations or embedding)
Written as an HTML table to site-outputs/tables/ (for the website)
Stored as an .rds object in the cache/ directory
The results pages show the HTML copies. They are produced by gt::as_raw_html(), so the website shows the same formatted table as the manuscript files, and the site can be built without gt or the cached objects. site-outputs/ is committed and public, so review changes to it for disclosure before committing.
The .rds objects are kept for reloading the formatted tables in an R session; the website does not read them.
Why This Matters
This step translates analytic outputs into:
Clean, interpretable manuscript tables
Reproducible formatted results
Files ready for journal submission
It represents the final transformation from geographic modeling to publication-ready evidence.
Click to show/hide R Code
#Tables# Initialize lists of gt objectsResults_HIV_gt<-list()Results_nonHIV_gt<-list()# Folder for the tables the website shows. It is committed and published, so# check any change to it for disclosure before committing.dir.create("site-outputs/tables", recursive =TRUE, showWarnings =FALSE)#Create tables as docx and PNG, and as HTML for the websitefor (organ_loop in organ_list){#Initialize file_path file_path<-list() file_path[["Table 1"]][[".docx"]]<-paste0("tables/", organ_loop,"/",organ_loop, " analysis results Table 1.docx") file_path[["Table 2"]][[".docx"]]<-paste0("tables/", organ_loop,"/",organ_loop, " analysis results Table 2.docx") file_path[["Table 1"]][[".png"]]<-paste0("tables/", organ_loop,"/",organ_loop, " analysis results Table 1.png") file_path[["Table 2"]][[".png"]]<-paste0("tables/", organ_loop,"/",organ_loop, " analysis results Table 2.png")#Define table with labels based on values in distance_list and types of outcomes table_factor_values<-expand.grid(Type =c("active", "HIV", "HOPE"),Distance = distance_list,stringsAsFactors =FALSE)%>%mutate(value=paste(Type, Distance))%>%mutate(label =paste(recode(Type,active =paste("Active", organ_loop, "center"),HIV =paste("Active HIV R+", organ_loop, "center"),HOPE =paste("Active HOPE D+", organ_loop, "center") ),case_when( stringr::str_detect(Distance, "mile$") ~ stringr::str_replace(Distance, "mile$", " mile radius"), stringr::str_detect(Distance, "min$") ~ stringr::str_replace(Distance, "min$", " min radius"),TRUE~ Distance ) ) )#Remove .docx files if they existif (file.exists(file_path[["Table 1"]][[".docx"]])) {file.remove(file_path[["Table 1"]][[".docx"]]) }if (file.exists(file_path[["Table 2"]][[".docx"]])) {file.remove(file_path[["Table 2"]][[".docx"]]) }#Create tables and save results as .docx and PNG Results_HIV_gt[[organ_loop]]<-Results_HIV_df[[organ_loop]]%>%convert_resultsdf_to_table(year_list_fn=year_list,row_values = table_factor_values$value,row_labels = table_factor_values$label,table_title="Access to transplant in PLWH")gtsave(Results_HIV_gt[[organ_loop]], file_path[["Table 1"]][[".docx"]])gtsave(Results_HIV_gt[[organ_loop]], file_path[["Table 1"]][[".png"]])writeLines(as_raw_html(Results_HIV_gt[[organ_loop]]),paste0("site-outputs/tables/", organ_loop, "_hiv.html")) Results_nonHIV_gt[[organ_loop]]<-Results_nonHIV_df[[organ_loop]]%>%convert_resultsdf_to_table(year_list_fn=year_list,row_values = table_factor_values$value,row_labels = table_factor_values$label,table_title="Access to transplant in people without HIV")gtsave(Results_nonHIV_gt[[organ_loop]], file_path[["Table 2"]][[".docx"]])gtsave(Results_nonHIV_gt[[organ_loop]], file_path[["Table 2"]][[".png"]])writeLines(as_raw_html(Results_nonHIV_gt[[organ_loop]]),paste0("site-outputs/tables/", organ_loop, "_nonhiv.html"))}# Save the gt objects for use in an R session. The website does not read# these; it shows the HTML copies in site-outputs/tables/.saveRDS(Results_HIV_gt, "cache/Results_HIV_gt.rds")saveRDS(Results_nonHIV_gt, "cache/Results_nonHIV_gt.rds")
---title: "Tables"format: html---## Table 1 and Table 2: Generating Manuscript-Ready TablesThis section formats the aggregated results into publication-ready tables using the [{gt}](https://gt.rstudio.com/) package.Earlier steps produced:- `Results_HIV_df` — coverage results for people living with HIV (PLWH)- `Results_nonHIV_df` — coverage results for people without HIVHere, those summary data frames are transformed into formatted tables and exported for manuscript use.---::: {.Rcode title="Source code"}The full R script is available at:- [`R/tables.R`](https://github.com/VagishHemmige/HOPE-CDC-analysis-2026/blob/master/R/tables.R)The code sections below are imported directly from that script when this page is rendered.This R script file is itself reliant on the following helper files:- [`R/setup.R`](https://github.com/VagishHemmige/HOPE-CDC-analysis-2026/blob/master/R/setup.R)- [`R/functions.R`](https://github.com/VagishHemmige/HOPE-CDC-analysis-2026/blob/master/R/functions.R):::---## What This Section DoesFor each organ:1. Constructs descriptive row labels (e.g., “Active kidney center, 60 min radius”).2. Converts the results data frame into a formatted {gt} table.3. Saves the tables as: - `.docx` (for manuscript editing) - `.png` (for quick sharing or embedding)4. Writes an HTML copy of each table to `site-outputs/tables/` for the website.5. Caches the final {gt} objects as `.rds` files for use in an R session.---## Step 1: Define Output File PathsFor each organ, file paths are dynamically generated for:- Table 1 (PLWH results)- Table 2 (Non-HIV population results)Each table is saved in both:- Word format (`.docx`)- Image format (`.png`)Existing `.docx` files are removed first to avoid overwrite conflicts.---## Step 2: Create Descriptive Row LabelsThe script builds a lookup table (`table_factor_values`) that:- Combines center type (`active`, `HIV`, `HOPE`)- Combines distance definitions (e.g., 50mile, 60min)- Converts internal codes into human-readable labelsExamples:- `active 60min` → “Active kidney center 60 min radius”- `HIV 50mile` → “Active HIV R+ kidney center 50 mile radius”This ensures consistent, manuscript-friendly labeling.---## Step 3: Convert Results to `gt` TablesThe helper function `convert_resultsdf_to_table()`:- Orders rows consistently- Formats percentages- Applies table titles- Structures the output by yearTwo tables are created per organ:- **Table 1:** Access to transplant in PLWH - **Table 2:** Access to transplant in people without HIV ---## Step 4: Export and CacheEach table is:- Saved as `.docx` (for manuscript submission workflows)- Saved as `.png` (for presentations or embedding)- Written as an HTML table to `site-outputs/tables/` (for the website)- Stored as an `.rds` object in the `cache/` directoryThe results pages show the HTML copies. They are produced by `gt::as_raw_html()`, so the website shows the same formatted table as the manuscript files, and the site can be built without gt or the cached objects. `site-outputs/` is committed and public, so review changes to it for disclosure before committing.The `.rds` objects are kept for reloading the formatted tables in an R session; the website does not read them.---## Why This MattersThis step translates analytic outputs into:- Clean, interpretable manuscript tables- Reproducible formatted results- Files ready for journal submissionIt represents the final transformation from geographic modeling to publication-ready evidence.```{r}#| file: "../R/tables.R"#| eval: false```