Generate Dynamic Text Results with glue and lapply in R
🧩 Introduction
When presenting analysis results, it’s often necessary to embed specific values into structured sentences — for example, describing results for certain patients, countries, or hospitals.
The glue
package in R provides a powerful solution for this. It allows you to integrate variable values into a fixed text template, making your code more efficient, readable, and less prone to errors.
In this post, I’ll demonstrate how to: - Use glue()
to dynamically create descriptive text
- Use glue_collapse()
to collapse grouped records
- Use lapply()
to manage and prefix multiple datasets
- Integrate the results into Quarto or R Markdown reports
📦 Required Packages
library(dplyr)
library(glue)
🪇 Step 1: Create and Prefix Multiple Datasetsrepresenting different hospitals’ medication records:
# Sample CM data from different hospitals
<- data.frame(USUBJID = c("001", "002"), CMTRT = c("DrugA", "DrugB"))
cm1 <- data.frame(USUBJID = c("003", "004"), CMTRT = c("DrugC", "DrugD"))
cm2
# Put into a named list
<- list(Hosp1 = cm1, Hosp2 = cm2)
cm_list
# Prefix each dataset with its group name
<- lapply(names(cm_list), function(name) {
cm_prefixed <- cm_list[[name]]
df $Group <- name
df
df
})
# Merge all into one
<- bind_rows(cm_prefixed) cm_all
🧠 Step 2: Collapse Records per Subject
We want to describe each subject’s treatment history by combining multiple CMTRT values:
# Example: combine multiple treatments per subject
<- cm_all %>%
cm_text group_by(USUBJID, Group) %>%
summarise(cmx_CMTRT = glue_collapse(CMTRT, sep = "; "), .groups = "drop")
✨ Step 3: Use glue()
to Format Sentences
# Create dynamic sentences for reporting
<- cm_text %>%
cm_text mutate(sentence = glue("Subject {USUBJID} in {Group} was treated with: {cmx_CMTRT}."))
# Preview
$sentence cm_text
📝 Output Example
001 in Hosp1 was treated with: DrugA.
Subject 002 in Hosp1 was treated with: DrugB.
Subject 003 in Hosp2 was treated with: DrugC.
Subject 004 in Hosp2 was treated with: DrugD. Subject
🧵 Conclusion
The combination of glue(), glue_collapse(), and lapply() offers a powerful workflow for:
fficient text generation
Flexible dataset processing
Clean report integration
This approach not only reduces manual effort but also ensures consistency and clarity in reporting.