Code
#| output: false
#| warning: false
rm(list=ls(all=TRUE))
library(patientProfilesVis)
library(pander)
library(ggplot2)
library(plotly)
library(dplyr)
library(htmlwidgets)
library(htmltools)
#| output: false
#| warning: false
rm(list=ls(all=TRUE))
library(patientProfilesVis)
library(pander)
library(ggplot2)
library(plotly)
library(dplyr)
library(htmlwidgets)
library(htmltools)
library(clinUtils)
# import example data:
data(dataSDTMCDISCP01)
# formatted as a list of data.frame (one per domain)
<- dataSDTMCDISCP01
dataSDTM names(dataSDTM)
<- attr(dataSDTM, "labelVars")
labelVarsSDTM head(labelVarsSDTM)
<- dataSDTM$AE
dataAE
# write.csv(dataAE, file="C:\\Users\\echen\\Documents\\EC\\Rcode\\PatProfile\\dataAE.csv")
# sort severities
"AESEV"] <- factor(dataAE[, "AESEV"], levels = c("MILD", "MODERATE", "SEVERE")) dataAE[,
=12
font_size# Create a named vector for severity colors
<- c("MILD" ="black" , "MODERATE" = "brown", "SEVERE" = "red")
severity_colors
#### R function for adverse event plot#######
<- function(subject_id,font_size = 12, save_plot = FALSE) {
createAdverseEventPlot <- dataAE %>% filter(USUBJID == subject_id)
subject_data
# Create the ggplot
<- ggplot(subject_data, aes(x = AESTDY, xend = AEENDY, y = AETERM, yend = AETERM, color = AESEV)) +
p geom_segment(size = 2, lineend = "round") +
geom_text(aes(x = AESTDY, label = "S"), size = 4, family = "Arial Unicode MS",fontface='bold') +
geom_text(aes(x = AEENDY, label = "E"), size = 4, family = "Arial Unicode MS",fontface='bold') +
scale_color_manual(values = severity_colors) +
labs(title = paste("Adverse Events Timeline for Patient", subject_id),
x = "Study Day", y = "Adverse Events", color = "Severity") +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(color = "grey80"),
axis.text.y = element_text(hjust = 0, size = font_size),
axis.text.x = element_text(size = font_size),
axis.title.x = element_text(size = font_size),
axis.title.y = element_text(size = font_size),
plot.title = element_text(size = font_size, hjust = 0.5),
legend.text = element_text(size = font_size),
legend.title = element_text(size = font_size))
return(p)
}
# Get the unique subject IDs
# subject_ids <- unique(dataAE$USUBJID)
<- unique(dataAE$USUBJID)[1]
subject_id <- ggplotly(createAdverseEventPlot(subject_id), width=1800, height=600)
plotly_object # Save the plot as an HTML widget
<- tempfile(fileext = ".html")
temp_file saveWidget(plotly_object, temp_file, selfcontained = TRUE)
# Embed the HTML widget in the output document
includeHTML(temp_file)
<- unique(dataAE$USUBJID)[2]
subject_id <- ggplotly(createAdverseEventPlot(subject_id), width=1800, height=600)
plotly_object # Save the plot as an HTML widget
<- tempfile(fileext = ".html")
temp_file saveWidget(plotly_object, temp_file, selfcontained = TRUE)
# Embed the HTML widget in the output document
includeHTML(temp_file)
<- unique(dataAE$USUBJID)[3]
subject_id <- ggplotly(createAdverseEventPlot(subject_id), width=1800, height=600)
plotly_object # Save the plot as an HTML widget
<- tempfile(fileext = ".html")
temp_file saveWidget(plotly_object, temp_file, selfcontained = TRUE)
# Embed the HTML widget in the output document
includeHTML(temp_file)
<- unique(dataAE$USUBJID)[4]
subject_id <- ggplotly(createAdverseEventPlot(subject_id), width=1800, height=600)
plotly_object # Save the plot as an HTML widget
<- tempfile(fileext = ".html")
temp_file saveWidget(plotly_object, temp_file, selfcontained = TRUE)
# Embed the HTML widget in the output document
includeHTML(temp_file)
# Get the unique subject IDs
<- unique(dataAE$USUBJID)
subject_ids
# Create a list of plotly objects for a subset of subject IDs
<- lapply(subject_ids[1:7], function(subject_id) {
plots
<- dataAE %>% filter(USUBJID == subject_id)
subject_data
# Create the ggplot
<- ggplot(subject_data, aes(x = AESTDY, xend = AEENDY, y = AETERM, yend = AETERM, color = AESEV)) +
p geom_segment(size = 2, lineend = "round") +
geom_text(aes(x = AESTDY, label = "S"), size = 4, family = "Arial Unicode MS",fontface='bold') +
geom_text(aes(x = AEENDY, label = "E"), size = 4, family = "Arial Unicode MS",fontface='bold') +
scale_color_manual(values = severity_colors) +
labs(title = paste("Adverse Events Timeline for Patient", subject_id),
x = "Study Day", y = "Adverse Events", color = "Severity") +
theme_minimal() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_line(color = "grey80"),
axis.text.y = element_text(hjust = 0, size = font_size),
axis.text.x = element_text(size = font_size),
axis.title.x = element_text(size = font_size),
axis.title.y = element_text(size = font_size),
plot.title = element_text(size = font_size, hjust = 0.5),
legend.text = element_text(size = font_size),
legend.title = element_text(size = font_size))
# ggplotly(p, width = 1800, height = 500)
ggplotly(p)
})
# Use htmltools to render the plots
::tagList(plots) htmltools
set.seed(159159) # Create example data
<- data.frame(x = 1:100,
data y1 = rnorm(100),
y2 = rnorm(100),
y3 = rnorm(100))
# for(i in 2:ncol(data)) { # ggplot within for-loop
# print(ggplotly(ggplot(data, aes(x = x, y = data[ , i])) +
# geom_point()))
#
# }
<- lapply(2:ncol(data), function(i) {
plots <- ggplot(data, aes_string(x = "x", y = names(data)[i])) +
p geom_point()
ggplotly(p)
})
# Use htmltools to render the plots
::tagList(plots) htmltools
https://quarto.org/docs/authoring/article-layout.html