---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` 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)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. Caches the final `gt` objects as `.rds` files for reuse in Quarto.---## 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)- Stored as an `.rds` object in the `cache/` directoryCaching the `gt` objects allows Quarto pages to load the formatted tables directly without regenerating 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, eval=FALSE}#Tables# Initialize lists of gt objectsResults_HIV_gt<-list()Results_nonHIV_gt<-list()#Create tables as docx and PNGfor (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 exist if (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"]]) 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"]])}#Save table objects as .RDS so they can be accessed by QuartosaveRDS(Results_HIV_gt, "cache/Results_HIV_gt.rds")saveRDS(Results_nonHIV_gt, "cache/Results_nonHIV_gt.rds")```## Other portions of the analysis- [**Setup**](setup.qmd): Defines global paths, data sources, cohort inclusion criteria, and analysis-wide constants.- [**Functions**](functions.qmd): Reusable helper functions for cohort construction, matching, costing, and modeling.- [**Figures**](figures.qmd):Visualizations of costs, risks, and model-based estimates.- [**About**](about.qmd): methods, assumptions, and disclosures