Skip to content

Commit f1b5d51

Browse files
committed
feat: allow the user to pass custom colors and symbols
1 parent 4137aa1 commit f1b5d51

File tree

2 files changed

+47
-72
lines changed

2 files changed

+47
-72
lines changed

R/tm_p_swimlane2.r

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
tm_p_swimlane2 <- function(label = "Swimlane Plot Module", plotly_specs, title) {
1+
tm_p_swimlane2 <- function(label = "Swimlane Plot Module", plotly_specs, title, colors = c(), symbols = c()) {
22
module(
33
label = label,
44
ui = ui_p_swimlane2,
55
server = srv_p_swimlane2,
66
datanames = "all",
77
server_args = list(
88
plotly_specs = plotly_specs,
9-
title = title
9+
title = title,
10+
colors = colors,
11+
symbols = symbols
1012
)
1113
)
1214
}
@@ -27,10 +29,12 @@ srv_p_swimlane2 <- function(id,
2729
data,
2830
plotly_specs,
2931
title = "Swimlane plot",
32+
colors,
33+
symbols,
3034
filter_panel_api) {
3135
moduleServer(id, function(input, output, session) {
3236
plotly_q <- reactive({
33-
plotly_call <- .make_plotly_call(specs = plotly_specs)
37+
plotly_call <- .make_plotly_call(specs = plotly_specs, colors = colors, symbols = symbols)
3438
code <- substitute(
3539
p <- plotly_call,
3640
list(plotly_call = plotly_call)
@@ -76,20 +80,20 @@ srv_p_swimlane2 <- function(id,
7680

7781

7882

79-
.make_plotly_call <- function(init_call = quote(plotly::plot_ly()), specs) {
83+
.make_plotly_call <- function(specs, colors = c(), symbols = c()) {
84+
init_call <- substitute(plotly::plot_ly(colors = colors, symbols = symbols), list(colors = colors, symbols = symbols))
8085
points_calls <- lapply(specs, function(x) {
8186
which_fun <- c(which(names(x) == "fun"), 1)[1]
8287
if (is.character(x[[which_fun]])) {
8388
x[[which_fun]] <- str2lang(x[[which_fun]])
8489
}
85-
basic_call <- as.call(
90+
as.call(
8691
c(
8792
list(x[[which_fun]]),
8893
x[-which_fun]
8994
)
9095
)
9196
})
92-
9397
rhs <- Reduce(
9498
x = c(init_call, points_calls),
9599
f = function(x, y) call("%>%", x, y)

inst/poc_crf.R

Lines changed: 37 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ pkgload::load_all("teal")
22
pkgload::load_all("teal.widgets")
33
pkgload::load_all("teal.modules.general")
44

5-
# Example data
5+
# Note: Please add the `PATH_TO_DATA` and change the X, Y, and Z Administrations to the actual values in the data
6+
67
data <- within(teal_data(), {
78
library(dplyr)
89
library(arrow)
@@ -32,79 +33,49 @@ data <- within(teal_data(), {
3233
summarise(max_study_day = max(event_study_day))
3334
})
3435

35-
color_manual <- c(
36-
"DEATH" = "black",
37-
"WITHDRAWAL BY SUBJECT" = "grey",
38-
"PD (Progressive Disease)" = "red",
39-
"SD (Stable Disease)" = "darkorchid4",
40-
"MR (Minimal/Minor Response)" = "sienna4",
41-
"PR (Partial Response)" = "maroon",
42-
"VGPR (Very Good Partial Response)" = "chartreuse4",
43-
"CR (Complete Response)" = "#3a41fc",
44-
"SCR (Stringent Complete Response)" = "midnightblue"
45-
)
46-
shape_manual <- c(
47-
"DEATH" = 4,
48-
"WITHDRAWAL BY SUBJECT" = 5,
49-
"PD (Progressive Disease)" = 8,
50-
"SD (Stable Disease)" = 5,
51-
"MR (Minimal/Minor Response)" = 5,
52-
"PR (Partial Response)" = 5,
53-
"VGPR (Very Good Partial Response)" = 5,
54-
"CR (Complete Response)" = 5,
55-
"SCR (Stringent Complete Response)" = 5
36+
plotly_specs <- list(
37+
list("plotly::add_markers", x = ~study_day, y = ~subject, color = ~catagory, symbol = ~catagory, data = quote(study_drug_administration)),
38+
list("plotly::add_markers", x = ~study_day, y = ~subject, color = ~catagory, symbol = ~catagory, data = quote(response_assessment)),
39+
list("plotly::add_markers", x = ~study_day, y = ~subject, color = ~catagory, symbol = ~catagory, data = quote(disposition)),
40+
list("plotly::add_bars", x = ~max_study_day, y = ~subject, data = quote(max_subject_day), width = 0.1, marker = list(color = "grey"), showlegend = FALSE)
5641
)
5742

5843
app <- init(
5944
data = data,
6045
modules = modules(
6146
tm_data_table(),
62-
tm_p_swimlane(
47+
tm_p_swimlane2(
6348
label = "Swimlane",
64-
geom_specs = list(
65-
list(
66-
geom = str2lang("ggplot2::geom_bar"),
67-
data = quote(max_subject_day),
68-
mapping = list(y = quote(subject), x = quote(max_study_day)),
69-
stat = "identity",
70-
width = 0.1
71-
),
72-
list(
73-
geom = quote(geom_point),
74-
data = quote(study_drug_administration),
75-
mapping = list(
76-
y = quote(subject), x = quote(study_day), color = quote(catagory), shape = quote(catagory)
77-
)
78-
),
79-
list(
80-
geom = quote(geom_point),
81-
data = quote(disposition),
82-
mapping = list(
83-
y = quote(subject), x = quote(study_day), color = quote(catagory), shape = quote(catagory)
84-
)
85-
),
86-
list(
87-
geom = quote(geom_point),
88-
data = quote(response_assessment),
89-
mapping = list(
90-
y = quote(subject), x = quote(study_day), color = quote(catagory), shape = quote(catagory)
91-
)
92-
),
93-
list(
94-
geom = quote(scale_color_manual),
95-
values = color_manual,
96-
breaks = names(color_manual)
97-
),
98-
list(
99-
geom = quote(scale_shape_manual),
100-
values = shape_manual,
101-
breaks = names(shape_manual)
102-
),
103-
list(
104-
geom = quote(theme_minimal)
105-
)
49+
plotly_specs = plotly_specs,
50+
title = "Swimlane Efficacy Plot",
51+
colors = c(
52+
"DEATH" = "black",
53+
"WITHDRAWAL BY SUBJECT" = "grey",
54+
"PD (Progressive Disease)" = "red",
55+
"SD (Stable Disease)" = "darkorchid4",
56+
"MR (Minimal/Minor Response)" = "sienna4",
57+
"PR (Partial Response)" = "maroon",
58+
"VGPR (Very Good Partial Response)" = "chartreuse4",
59+
"CR (Complete Response)" = "#3a41fc",
60+
"SCR (Stringent Complete Response)" = "midnightblue",
61+
"X Administration Injection" = "goldenrod",
62+
"Y Administration Infusion" = "deepskyblue3",
63+
"Z Administration Infusion" = "darkorchid"
10664
),
107-
title = "Swimlane Efficacy Plot"
65+
symbols = c(
66+
"DEATH" = "circle",
67+
"WITHDRAWAL BY SUBJECT" = "square",
68+
"PD (Progressive Disease)" = "circle",
69+
"SD (Stable Disease)" = "square-open",
70+
"MR (Minimal/Minor Response)" = "star-open",
71+
"PR (Partial Response)" = "star-open",
72+
"VGPR (Very Good Partial Response)" = "star-open",
73+
"CR (Complete Response)" = "star-open",
74+
"SCR (Stringent Complete Response)" = "star-open",
75+
"X Administration Injection" = "line-ns-open",
76+
"Y Administration Infusion" = "line-ns-open",
77+
"Z Administration Infusion" = "line-ns-open"
78+
)
10879
)
10980
)
11081
)

0 commit comments

Comments
 (0)