Skip to content

Commit f4a13ef

Browse files
committed
feat: add MVP version of line and barplot
1 parent de3b770 commit f4a13ef

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ export(tm_g_scatterplot)
2323
export(tm_g_scatterplotmatrix)
2424
export(tm_missing_data)
2525
export(tm_outliers)
26+
export(tm_p_bargraph)
27+
export(tm_p_lineplot)
2628
export(tm_p_scatterplot)
2729
export(tm_p_spiderplot)
2830
export(tm_p_swimlane)

R/tm_p_bargraph.R

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#' @export
2+
tm_p_bargraph <- function(label = "Bar Plot",
3+
plot_dataname,
4+
y_var,
5+
color_var,
6+
count_var,
7+
bar_colors = NULL) {
8+
module(
9+
label = label,
10+
ui = ui_p_bargraph,
11+
server = srv_p_bargraph,
12+
ui_args = list(),
13+
server_args = list(
14+
plot_dataname = plot_dataname,
15+
y_var = y_var,
16+
color_var = color_var,
17+
count_var = count_var,
18+
bar_colors = bar_colors
19+
)
20+
)
21+
}
22+
23+
ui_p_bargraph <- function(id) {
24+
ns <- NS(id)
25+
bslib::page_fluid(
26+
bslib::card(
27+
full_screen = TRUE,
28+
tags$div(
29+
# trigger_tooltips_deps(),
30+
plotly::plotlyOutput(ns("plot"), height = "100%")
31+
)
32+
)
33+
)
34+
}
35+
36+
srv_p_bargraph <- function(id, data, plot_dataname, y_var, color_var, count_var, bar_colors) {
37+
moduleServer(id, function(input, output, session) {
38+
plotly_q <- reactive({
39+
df <- data()[[plot_dataname]]
40+
df[[color_var]] <- as.character(df[[color_var]])
41+
42+
plot_data <- df %>%
43+
group_by(!!as.name(y_var), !!as.name(color_var)) %>%
44+
summarize(count = n_distinct(!!as.name(count_var)), .groups = "drop")
45+
46+
event_type_order <- plot_data %>%
47+
group_by(!!as.name(y_var)) %>%
48+
summarize(total = sum(count)) %>%
49+
arrange(total) %>%
50+
pull(!!as.name(y_var))
51+
52+
plot_data[[y_var]] <- factor(plot_data[[y_var]], levels = event_type_order)
53+
54+
p <- plot_ly(
55+
data = plot_data,
56+
y = as.formula(paste0("~", y_var)),
57+
x = ~count,
58+
color = as.formula(paste0("~", color_var)),
59+
colors = bar_colors,
60+
type = "bar",
61+
orientation = "h"
62+
) %>%
63+
layout(
64+
barmode = "stack",
65+
xaxis = list(title = "Count"),
66+
yaxis = list(title = "Adverse Event Type"),
67+
legend = list(title = list(text = "AE Type"))
68+
)
69+
70+
p
71+
})
72+
73+
74+
output$plot <- plotly::renderPlotly({
75+
p <- plotly_q()
76+
plotly::event_register(p, "plotly_selected")
77+
p
78+
})
79+
})
80+
}

R/tm_p_lineplot.R

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#' @export
2+
tm_p_lineplot <- function(label = "Line Plot",
3+
plot_dataname,
4+
x_var,
5+
y_var,
6+
transformators = list()) {
7+
module(
8+
label = label,
9+
ui = ui_p_lineplot,
10+
server = srv_p_lineplot,
11+
ui_args = list(),
12+
server_args = list(
13+
plot_dataname = plot_dataname,
14+
x_var = x_var,
15+
y_var = y_var
16+
),
17+
transformators = transformators
18+
)
19+
}
20+
21+
ui_p_lineplot <- function(id) {
22+
ns <- NS(id)
23+
bslib::page_fluid(
24+
tags$div(
25+
# trigger_tooltips_deps(),
26+
plotly::plotlyOutput(ns("plot"), height = "100%")
27+
)
28+
)
29+
}
30+
31+
srv_p_lineplot <- function(id, data, plot_dataname, x_var, y_var) {
32+
moduleServer(id, function(input, output, session) {
33+
plotly_q <- reactive({
34+
df <- data()[[plot_dataname]]
35+
36+
validate(need(nrow(df) > 0, "No data after applying filters."))
37+
38+
# TODO: implement the high/low lines with annotations
39+
y_low_last <- if ("si_low" %in% names(df)) utils::tail(stats::na.omit(df[["si_low"]]), 1) else NA
40+
y_high_last <- if ("si_high" %in% names(df)) utils::tail(stats::na.omit(df[["si_high"]]), 1) else NA
41+
42+
p <- plotly::plot_ly(data = df, x = df[[x_var]]) |>
43+
plotly::add_trace(
44+
y = df[[y_var]],
45+
mode = "lines+markers", type = "scatter", name = "Lab Result",
46+
line = list(color = "green"),
47+
marker = list(color = "green"),
48+
showlegend = FALSE
49+
) |>
50+
# plotly::add_trace(
51+
# y = df[["si_low"]],
52+
# mode = "lines",
53+
# line = list(color = "red", dash = "dash"),
54+
# showlegend = FALSE
55+
# ) |>
56+
# plotly::add_annotations(
57+
# x = max(df[[x_var]], na.rm = TRUE),
58+
# y = y_low_last,
59+
# yshift = 15,
60+
# text = "Original LLN",
61+
# showarrow = FALSE
62+
# ) |>
63+
# plotly::add_trace(
64+
# y = df[["si_high"]],
65+
# mode = "lines",
66+
# line = list(color = "red", dash = "solid"),
67+
# showlegend = FALSE
68+
# ) |>
69+
# plotly::add_annotations(
70+
# x = max(df[[x_var]], na.rm = TRUE),
71+
# y = y_high_last,
72+
# yshift = -15,
73+
# text = "Original ULN",
74+
# showarrow = FALSE
75+
# ) |>
76+
plotly::layout(
77+
xaxis = list(title = "Study Day of Sample Collection", zeroline = FALSE),
78+
yaxis = list(title = "Original Result")
79+
)
80+
81+
p
82+
})
83+
84+
85+
output$plot <- plotly::renderPlotly({
86+
p <- plotly_q()
87+
plotly::event_register(p, "plotly_selected")
88+
p
89+
})
90+
})
91+
}

0 commit comments

Comments
 (0)