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)
dataSDTM <- dataSDTMCDISCP01
names(dataSDTM)
labelVarsSDTM <- attr(dataSDTM, "labelVars")
head(labelVarsSDTM) dataAE <- dataSDTM$AE
# write.csv(dataAE, file="C:\\Users\\echen\\Documents\\EC\\Rcode\\PatProfile\\dataAE.csv")
# sort severities
dataAE[, "AESEV"] <- factor(dataAE[, "AESEV"], levels = c("MILD", "MODERATE", "SEVERE"))font_size=12
# Create a named vector for severity colors
severity_colors <- c("MILD" ="black" , "MODERATE" = "brown", "SEVERE" = "red")
#### R function for adverse event plot#######
createAdverseEventPlot <- function(subject_id,font_size = 12, save_plot = FALSE) {
subject_data <- dataAE %>% filter(USUBJID == subject_id)
# Create the ggplot
p <- ggplot(subject_data, aes(x = AESTDY, xend = AEENDY, y = AETERM, yend = AETERM, color = AESEV)) +
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)
subject_id <- unique(dataAE$USUBJID)[1]
plotly_object <- ggplotly(createAdverseEventPlot(subject_id), width=1800, height=600)
# Save the plot as an HTML widget
temp_file <- tempfile(fileext = ".html")
saveWidget(plotly_object, temp_file, selfcontained = TRUE)
# Embed the HTML widget in the output document
includeHTML(temp_file)subject_id <- unique(dataAE$USUBJID)[2]
plotly_object <- ggplotly(createAdverseEventPlot(subject_id), width=1800, height=600)
# Save the plot as an HTML widget
temp_file <- tempfile(fileext = ".html")
saveWidget(plotly_object, temp_file, selfcontained = TRUE)
# Embed the HTML widget in the output document
includeHTML(temp_file)subject_id <- unique(dataAE$USUBJID)[3]
plotly_object <- ggplotly(createAdverseEventPlot(subject_id), width=1800, height=600)
# Save the plot as an HTML widget
temp_file <- tempfile(fileext = ".html")
saveWidget(plotly_object, temp_file, selfcontained = TRUE)
# Embed the HTML widget in the output document
includeHTML(temp_file)subject_id <- unique(dataAE$USUBJID)[4]
plotly_object <- ggplotly(createAdverseEventPlot(subject_id), width=1800, height=600)
# Save the plot as an HTML widget
temp_file <- tempfile(fileext = ".html")
saveWidget(plotly_object, temp_file, selfcontained = TRUE)
# Embed the HTML widget in the output document
includeHTML(temp_file)# Get the unique subject IDs
subject_ids <- unique(dataAE$USUBJID)
# Create a list of plotly objects for a subset of subject IDs
plots <- lapply(subject_ids[1:7], function(subject_id) {
subject_data <- dataAE %>% filter(USUBJID == subject_id)
# Create the ggplot
p <- ggplot(subject_data, aes(x = AESTDY, xend = AEENDY, y = AETERM, yend = AETERM, color = AESEV)) +
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
htmltools::tagList(plots)set.seed(159159) # Create example data
data <- data.frame(x = 1:100,
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()))
#
# }
plots <- lapply(2:ncol(data), function(i) {
p <- ggplot(data, aes_string(x = "x", y = names(data)[i])) +
geom_point()
ggplotly(p)
})
# Use htmltools to render the plots
htmltools::tagList(plots)https://quarto.org/docs/authoring/article-layout.html