Setup
Package Installation
install.packages("tibble", type = "binary")
install.packages("dplyr", type = "binary")
install.packages("ggplot2", type = "binary")
install.packages("plotly", type = "binary")
install.packages("DT", type = "binary")
install.packages("htmlwidgets", type = "binary")
install.packages("pharmaverseadamjnj")Package Load
library(dplyr)
library(ggplot2)
library(plotly)
library(DT)
library(htmlwidgets)
library(pharmaverseadamjnj)Read in ADSL and carry out some data preparation
adsl <- pharmaverseadamjnj::adsl
adsl2 <- adsl %>%
select(USUBJID, TRT01P, TRTDURD, WEIGHTBL, HEIGHTBL) %>%
filter(!is.na(TRT01P) & !is.na(TRTDURD))
adsl3 <- adsl2 %>%
mutate_if(is.character, as.factor)
tf_colors <- c(
"Placebo" = "#0072B2",
"Xanomeline High Dose" = "#D55E00",
"Xanomeline Low Dose" = "#CC79A7"
)Recipe 1: Make A Figure Interactive
Boxplot: Create The Static Figure
f1 <- ggplot(data = adsl3, aes(x = TRT01P, y = TRTDURD, fill = TRT01P)) +
geom_boxplot() +
scale_x_discrete(name = "Treatment Group") +
scale_y_continuous(
limits = c(0, 250),
breaks = seq(0, 250, 50),
expand = c(0.05, 0.05),
name = "Treatment Duration (Days)"
) +
scale_fill_manual(
values = tf_colors,
name = "Treatment Group"
)Boxplot: Make It Interactive
g1 <- ggplotly(f1)
saveWidget(g1, file.path("interactive-outputs", "boxplotly.html"), selfcontained = TRUE)Scatterplot: Create The Static Figure
f2 <- ggplot(data = adsl3, aes(x = HEIGHTBL, y = WEIGHTBL, shape = TRT01P, colour = TRT01P)) +
geom_point() +
scale_x_continuous(
limits = c(0, 150),
breaks = seq(0, 150, 10),
name = "Baseline Height (cm)"
) +
scale_y_continuous(
limits = c(0, 150),
breaks = seq(0, 150, 10),
name = "Baseline Weight (kg)"
) +
scale_shape_manual(
values = c(16, 17, 15),
name = "Treatment Group"
) +
scale_colour_manual(
values = tf_colors,
name = "Treatment Group"
)Scatterplot: Make It Interactive
g2 <- ggplotly(f2)
saveWidget(g2, file.path("interactive-outputs", "scatterplotly.html"), selfcontained = TRUE)Recipe 2: Make A Table Explorable
Prepare The Table
my_table <- adsl3
my_table_2 <- my_table %>%
rename(
"Unique Subject Identifier" = USUBJID,
"Treatment Group" = TRT01P,
"Total Treatment Duration (Days)" = TRTDURD,
"Baseline Weight (kg)" = WEIGHTBL,
"Baseline Height (cm)" = HEIGHTBL
)Make It Interactive
d <- datatable(
my_table_2,
extensions = c("ColReorder"),
rownames = FALSE,
filter = "top",
class = "stripe hover compact",
options = list(
colReorder = TRUE,
pageLength = 5,
lengthMenu = c(5, 10, 12, 20, 50, 100)
)
)
saveWidget(d, file.path("interactive-outputs", "datatable.html"), selfcontained = TRUE)Recipe 3: Bring Outputs Together
Full Code
# PSI Conf 2026 interactivity recipes
install.packages("tibble", type = "binary")
install.packages("dplyr", type = "binary")
install.packages("ggplot2", type = "binary")
install.packages("plotly", type = "binary")
install.packages("DT", type = "binary")
install.packages("htmlwidgets", type = "binary")
install.packages("pharmaverseadamjnj")
library(dplyr)
library(ggplot2)
library(plotly)
library(DT)
library(htmlwidgets)
library(pharmaverseadamjnj)
# Data --------------------------------------------------------------------
adsl <- pharmaverseadamjnj::adsl
adsl2 <- adsl %>%
select(USUBJID, TRT01P, TRTDURD, WEIGHTBL, HEIGHTBL) %>%
filter(!is.na(TRT01P) & !is.na(TRTDURD))
adsl3 <- adsl2 %>%
mutate_if(is.character, as.factor)
tf_colors <- c(
"Placebo" = "#0072B2",
"Xanomeline High Dose" = "#D55E00",
"Xanomeline Low Dose" = "#CC79A7"
)
# Recipe 1: Make a figure interactive ------------------------------------
f1 <- ggplot(data = adsl3, aes(x = TRT01P, y = TRTDURD, fill = TRT01P)) +
geom_boxplot() +
scale_x_discrete(name = "Treatment Group") +
scale_y_continuous(
limits = c(0, 250),
breaks = seq(0, 250, 50),
expand = c(0.05, 0.05),
name = "Treatment Duration (Days)"
) +
scale_fill_manual(
values = tf_colors,
name = "Treatment Group"
)
g1 <- ggplotly(f1)
saveWidget(g1, file.path("interactive-outputs", "boxplotly.html"), selfcontained = TRUE)
f2 <- ggplot(data = adsl3, aes(x = HEIGHTBL, y = WEIGHTBL, shape = TRT01P, colour = TRT01P)) +
geom_point() +
scale_x_continuous(
limits = c(0, 150),
breaks = seq(0, 150, 10),
name = "Baseline Height (cm)"
) +
scale_y_continuous(
limits = c(0, 150),
breaks = seq(0, 150, 10),
name = "Baseline Weight (kg)"
) +
scale_shape_manual(
values = c(16, 17, 15),
name = "Treatment Group"
) +
scale_colour_manual(
values = tf_colors,
name = "Treatment Group"
)
g2 <- ggplotly(f2)
saveWidget(g2, file.path("interactive-outputs", "scatterplotly.html"), selfcontained = TRUE)
# Recipe 2: Make a table explorable --------------------------------------
my_table <- adsl3
my_table_2 <- my_table %>%
rename(
"Unique Subject Identifier" = USUBJID,
"Treatment Group" = TRT01P,
"Total Treatment Duration (Days)" = TRTDURD,
"Baseline Weight (kg)" = WEIGHTBL,
"Baseline Height (cm)" = HEIGHTBL
)
d <- datatable(
my_table_2,
extensions = c("ColReorder"),
rownames = FALSE,
filter = "top",
class = "stripe hover compact",
options = list(
colReorder = TRUE,
pageLength = 5,
lengthMenu = c(5, 10, 12, 20, 50, 100)
)
)
saveWidget(d, file.path("interactive-outputs", "datatable.html"), selfcontained = TRUE)