Skip to content

Commit 4027d2c

Browse files
committed
add refactored plot default vals logic
1 parent 4b774b2 commit 4027d2c

2 files changed

Lines changed: 117 additions & 73 deletions

File tree

R/utils-misc.R

Lines changed: 104 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -112,99 +112,130 @@ get_missing_plot_params <- function(plots) {
112112
# 3. check plots
113113

114114
plot_type_info <- list(
115-
range_plots <- list(
115+
range_plots = list(
116116
ae = list(regexp = "adverse events", v_plot_name = "Adverse Events Plot"),
117117
cm = list(regexp = "concomitant medication", v_plot_name = "Concomitant Medication Plot")
118118
),
119119
value_plots = list(
120-
lab = list(regexp = "^lab plot", dv_plot_name = "Lab plot"),
120+
lab = list(regexp = "lab plot", dv_plot_name = "Lab plot"),
121121
vital = list(regexp = "vital sign", v_plot_name = "Vital Sign Plot")
122122
)
123123
)
124124

125-
if (! rlang::has_name(plots, "range_plots")) {
126-
plots[["range_plots"]] <- v[["range_plots"]]
127-
} else {
128-
# i. check all range_plots
129-
#for (rp in )
130-
if (any(grepl("adverse event", names(plots[["range_plots"]]), ignore.case = TRUE))) {
131-
plot_name <- grep(
132-
"adverse event", names(plots[["range_plots"]]), ignore.case = TRUE, value = TRUE
133-
)
134-
for (i in plot_name) {
135-
for (j in names(range_plot_params)) {
136-
if (j %in% c("vars")) {
137-
for (var_col in m[["range_plots"]][["Adverse Events Plot"]][[j]]) {
138-
if (! rlang::has_name(plots[["range_plots"]][[i]][[j]], var_col)) {
139-
plots[["range_plots"]][[i]][[j]][[var_col]] <-
140-
v[["range_plots"]][["Adverse Events Plot"]][[j]][[var_col]]
141-
}
142-
}
143-
} else {
144-
if (! rlang::has_name(plots[["range_plots"]][[i]], j)) {
145-
plots[["range_plots"]][[i]][[j]] <- v[["range_plots"]][["Adverse Events Plot"]][[j]]
146-
}
147-
}
148-
}
149-
}
150-
}
151-
}
125+
plot_vars <- list(range_plots = m[["range_plots"]][[1]],
126+
value_plots = m[["value_plots"]][[1]])
152127

153-
if (! rlang::has_name(plots, "range_plots")) {
154-
plots[["range_plots"]] <- v[["range_plots"]]
155-
} else {
156-
range_plot_params <- m[["range_plots"]][[1]] # params are the same for AE, CM plots
157-
158-
# i. Adverse Event plot checks
159-
if (any(grepl("adverse event", names(plots[["range_plots"]]), ignore.case = TRUE))) {
160-
plot_name <- grep(
161-
"adverse event", names(plots[["range_plots"]]), ignore.case = TRUE, value = TRUE
162-
)
163-
for (i in plot_name) {
164-
for (j in names(range_plot_params)) {
165-
if (j %in% c("vars")) {
166-
for (var_col in m[["range_plots"]][["Adverse Events Plot"]][[j]]) {
167-
if (! rlang::has_name(plots[["range_plots"]][[i]][[j]], var_col)) {
168-
plots[["range_plots"]][[i]][[j]][[var_col]] <-
169-
v[["range_plots"]][["Adverse Events Plot"]][[j]][[var_col]]
170-
}
171-
}
172-
} else {
173-
if (! rlang::has_name(plots[["range_plots"]][[i]], j)) {
174-
plots[["range_plots"]][[i]][[j]] <- v[["range_plots"]][["Adverse Events Plot"]][[j]]
175-
}
176-
}
177-
}
178-
}
179-
}
128+
# x <- c("range_plots", "value_plots")
129+
# plot_type = x[2]
130+
# p = names(plot_type_info[[plot_type]])[1]
131+
132+
for (plot_type in c("range_plots", "value_plots")) {
133+
if (! rlang::has_name(plots, plot_type)) {
134+
plots[[plot_type]] <- v[[plot_type]]
135+
} else {
136+
137+
for (p in names(plot_type_info[[plot_type]])) {
138+
plot_details <- plot_type_info[[plot_type]][[p]]
139+
if (any(grepl(plot_details[["regexp"]], names(plots[[plot_type]]), ignore.case = TRUE))) {
140+
141+
plot_name <- grep(plot_details[["regexp"]], names(plots[[plot_type]]), ignore.case = TRUE, value = TRUE)
142+
143+
for (i in plot_name) {
144+
#i = plot_name
145+
print(i)
146+
for (j in names(plot_vars[[plot_type]])) {
147+
#j = "vars"
148+
print(j)
149+
if (j == "vars") {
150+
151+
missing_vars <- setdiff(plot_vars[[plot_type]][[j]],
152+
plots[[plot_type]][[i]][[j]] |> names())
153+
154+
if (length(missing_vars) > 0) {
155+
plots[[plot_type]][[i]][[j]] <- c(
156+
v[[plot_type]][[i]][[j]][missing_vars],
157+
plots[[plot_type]][[i]][[j]]
158+
)
180159

181-
# ii. Concomitant Medication Plot checks
182-
if (any(grepl("concomitant medication", names(plots[["range_plots"]]), ignore.case = TRUE))) {
183-
plot_name <- grep(
184-
"concomitant medication", names(plots[["range_plots"]]), ignore.case = TRUE, value = TRUE
185-
)
186-
for (i in plot_name) {
187-
for (j in names(range_plot_params)) {
188-
if (j %in% c("vars")) {
189-
for (var_col in m[["range_plots"]][["Concomitant Medication Plot"]][[j]]) {
190-
if (! rlang::has_name(plots[["range_plots"]][[i]][[j]], var_col)) {
191-
plots[["range_plots"]][[i]][[j]][[var_col]] <-
192-
v[["range_plots"]][["Concomitant Medication Plot"]][[j]][[var_col]]
193160
}
194-
}
195-
} else {
196-
if (! rlang::has_name(plots[["range_plots"]][[i]], j)) {
197-
plots[["range_plots"]][[i]][[j]] <- v[["range_plots"]][["Concomitant Medication Plot"]][[j]]
161+
# for (var_col in m[[plot_type]][[plot_details$v_plot_name]][[j]]) {
162+
# print(var_col)
163+
# if (! rlang::has_name(plots[[plot_type]][[i]][[j]], var_col)) {
164+
# plots[[plot_type]][[i]][[j]][[var_col]] <-
165+
# v[[plot_type]][[plot_details$v_plot_name]][[j]][[var_col]]
166+
# }
167+
# }
168+
} else {
169+
if (! rlang::has_name(plots[[plot_type]][[i]], j)) {
170+
plots[[plot_type]][[i]][[j]] <- v[[plot_type]][[plot_details$v_plot_name]][[j]]
171+
}
172+
}
198173
}
199174
}
175+
176+
200177
}
178+
201179
}
180+
202181
}
203182
}
204183

184+
# if (! rlang::has_name(plots, "range_plots")) {
185+
# plots[["range_plots"]] <- v[["range_plots"]]
186+
# } else {
187+
# range_plot_params <- m[["range_plots"]][[1]] # params are the same for AE, CM plots
188+
#
189+
# # i. Adverse Event plot checks
190+
# if (any(grepl("adverse event", names(plots[["range_plots"]]), ignore.case = TRUE))) {
191+
# plot_name <- grep(
192+
# "adverse event", names(plots[["range_plots"]]), ignore.case = TRUE, value = TRUE
193+
# )
194+
# for (i in plot_name) {
195+
# for (j in names(range_plot_params)) {
196+
# if (j %in% c("vars")) {
197+
# for (var_col in m[["range_plots"]][["Adverse Events Plot"]][[j]]) {
198+
# if (! rlang::has_name(plots[["range_plots"]][[i]][[j]], var_col)) {
199+
# plots[["range_plots"]][[i]][[j]][[var_col]] <-
200+
# v[["range_plots"]][["Adverse Events Plot"]][[j]][[var_col]]
201+
# }
202+
# }
203+
# } else {
204+
# if (! rlang::has_name(plots[["range_plots"]][[i]], j)) {
205+
# plots[["range_plots"]][[i]][[j]] <- v[["range_plots"]][["Adverse Events Plot"]][[j]]
206+
# }
207+
# }
208+
# }
209+
# }
210+
# }
211+
#
212+
# # ii. Concomitant Medication Plot checks
213+
# if (any(grepl("concomitant medication", names(plots[["range_plots"]]), ignore.case = TRUE))) {
214+
# plot_name <- grep(
215+
# "concomitant medication", names(plots[["range_plots"]]), ignore.case = TRUE, value = TRUE
216+
# )
217+
# for (i in plot_name) {
218+
# for (j in names(range_plot_params)) {
219+
# if (j %in% c("vars")) {
220+
# for (var_col in m[["range_plots"]][["Concomitant Medication Plot"]][[j]]) {
221+
# if (! rlang::has_name(plots[["range_plots"]][[i]][[j]], var_col)) {
222+
# plots[["range_plots"]][[i]][[j]][[var_col]] <-
223+
# v[["range_plots"]][["Concomitant Medication Plot"]][[j]][[var_col]]
224+
# }
225+
# }
226+
# } else {
227+
# if (! rlang::has_name(plots[["range_plots"]][[i]], j)) {
228+
# plots[["range_plots"]][[i]][[j]] <- v[["range_plots"]][["Concomitant Medication Plot"]][[j]]
229+
# }
230+
# }
231+
# }
232+
# }
233+
# }
234+
# }
235+
#
205236

206237
#check value_plots
207-
238+
return(plots)
208239

209240
}
210241
#' Apply default vals to plot list.

tests/testthat/test-data_prep.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,16 @@ test_that("pt_info_data_filter preserves data.frame labels", {
66
)
77
testthat::expect_identical(get_labels(output), expected_labels)
88
})
9+
10+
test_that("default vals are used for plots if any mandatory parameters are missing.", {
11+
12+
plots <- dv.papo:::CONST$plot_default_vals
13+
plots$range_plots$`Adverse Events Plot`$dataset <- NULL
14+
plots$range_plots$`Adverse Events Plot`$vars <- plots$range_plots$`Adverse Events Plot`$vars[2:5] #omit start_date
15+
plots$value_plots$`Lab plot`$tooltip <- NULL
16+
17+
plots_filled <- dv.papo:::get_missing_plot_params(plots)
18+
19+
20+
21+
})

0 commit comments

Comments
 (0)