Skip to content

Commit 8151b06

Browse files
averissimogogonzo
andauthored
Adds missing assertions and reorganizes them (#696)
# Pull Request - Fixes #686 - Part of #624 #### Changes descriptions - Adds missing assertions described on #686 - Adds a helper function called `assert_single_selection` - Avoids many repetitive calls across functions to check for multiple selections - Adds simple unit tests - Adds some comments to maintain a consistent structure --------- Signed-off-by: André Veríssimo <[email protected]> Co-authored-by: Dawid Kałędkowski <[email protected]>
1 parent cbae779 commit 8151b06

18 files changed

+567
-111
lines changed

R/tm_a_pca.R

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,38 @@ tm_a_pca <- function(label = "Principal Component Analysis",
106106
pre_output = NULL,
107107
post_output = NULL) {
108108
logger::log_info("Initializing tm_a_pca")
109+
110+
# Normalize the parameters
109111
if (inherits(dat, "data_extract_spec")) dat <- list(dat)
110112
if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args)
111113

114+
# Start of assertions
112115
checkmate::assert_string(label)
113116
checkmate::assert_list(dat, types = "data_extract_spec")
117+
118+
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE)
119+
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height")
120+
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE)
121+
checkmate::assert_numeric(
122+
plot_width[1],
123+
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width"
124+
)
125+
114126
ggtheme <- match.arg(ggtheme)
127+
128+
plot_choices <- c("Elbow plot", "Circle plot", "Biplot", "Eigenvector plot")
129+
checkmate::assert_list(ggplot2_args, types = "ggplot2_args")
130+
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
131+
115132
checkmate::assert_flag(rotate_xaxis_labels)
116133

134+
if (length(font_size) == 1) {
135+
checkmate::assert_numeric(font_size, any.missing = FALSE, finite = TRUE, lower = 8, upper = 20)
136+
} else {
137+
checkmate::assert_numeric(font_size, len = 3, any.missing = FALSE, finite = TRUE, lower = 8, upper = 20)
138+
checkmate::assert_numeric(font_size[1], lower = font_size[2], upper = font_size[3], .var.name = "font_size")
139+
}
140+
117141
if (length(alpha) == 1) {
118142
checkmate::assert_numeric(alpha, any.missing = FALSE, finite = TRUE, lower = 0, upper = 1)
119143
} else {
@@ -128,25 +152,11 @@ tm_a_pca <- function(label = "Principal Component Analysis",
128152
checkmate::assert_numeric(size[1], lower = size[2], upper = size[3], .var.name = "size")
129153
}
130154

131-
if (length(font_size) == 1) {
132-
checkmate::assert_numeric(font_size, any.missing = FALSE, finite = TRUE, lower = 8, upper = 20)
133-
} else {
134-
checkmate::assert_numeric(font_size, len = 3, any.missing = FALSE, finite = TRUE, lower = 8, upper = 20)
135-
checkmate::assert_numeric(font_size[1], lower = font_size[2], upper = font_size[3], .var.name = "font_size")
136-
}
137-
138-
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE)
139-
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height")
140-
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE)
141-
checkmate::assert_numeric(
142-
plot_width[1],
143-
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width"
144-
)
145-
146-
plot_choices <- c("Elbow plot", "Circle plot", "Biplot", "Eigenvector plot")
147-
checkmate::assert_list(ggplot2_args, types = "ggplot2_args")
148-
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
155+
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
156+
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
157+
# End of assertions
149158

159+
# Make UI args
150160
args <- as.list(environment())
151161

152162
data_extract_list <- list(dat = dat)

R/tm_a_regression.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ tm_a_regression <- function(label = "Regression Analysis",
162162
checkmate::assert_list(regressor, types = "data_extract_spec")
163163

164164
checkmate::assert_list(response, types = "data_extract_spec")
165-
if (!all(vapply(response, function(x) !(x$select$multiple), logical(1)))) {
166-
stop("'response' should not allow multiple selection")
167-
}
165+
assert_single_selection(response)
168166

169167
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE)
170168
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height")
@@ -203,7 +201,7 @@ tm_a_regression <- function(label = "Regression Analysis",
203201

204202
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
205203
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
206-
checkmate::assert_integerish(default_plot_type, lower = 1, upper = 7)
204+
checkmate::assert_choice(default_plot_type, seq.int(1L, length(plot_choices)))
207205
checkmate::assert_string(default_outlier_label)
208206

209207
if (length(label_segment_threshold) == 1) {
@@ -219,7 +217,7 @@ tm_a_regression <- function(label = "Regression Analysis",
219217
}
220218
# End of assertions
221219

222-
# Send ui args
220+
# Make UI args
223221
args <- as.list(environment())
224222
args[["plot_choices"]] <- plot_choices
225223
data_extract_list <- list(

R/tm_data_table.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ tm_data_table <- function(label = "Data Table",
9191
pre_output = NULL,
9292
post_output = NULL) {
9393
logger::log_info("Initializing tm_data_table")
94+
95+
# Start of assertions
9496
checkmate::assert_string(label)
97+
9598
checkmate::assert_list(variables_selected, min.len = 0, types = "character", names = "named")
9699
if (length(variables_selected) > 0) {
97100
lapply(seq_along(variables_selected), function(i) {
@@ -101,14 +104,17 @@ tm_data_table <- function(label = "Data Table",
101104
}
102105
})
103106
}
107+
104108
checkmate::assert_character(datasets_selected, min.len = 0, min.chars = 1)
105-
checkmate::assert_list(dt_options, names = "named")
106109
checkmate::assert(
107110
checkmate::check_list(dt_args, len = 0),
108111
checkmate::check_subset(names(dt_args), choices = names(formals(DT::datatable)))
109112
)
110-
113+
checkmate::assert_list(dt_options, names = "named")
111114
checkmate::assert_flag(server_rendering)
115+
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
116+
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
117+
# End of assertions
112118

113119
module(
114120
label,

R/tm_file_viewer.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,25 +42,24 @@
4242
tm_file_viewer <- function(label = "File Viewer Module",
4343
input_path = list("Current Working Directory" = ".")) {
4444
logger::log_info("Initializing tm_file_viewer")
45-
if (length(label) == 0 || identical(label, "")) {
46-
label <- " "
47-
}
48-
if (length(input_path) == 0 || identical(input_path, "")) {
49-
input_path <- list()
50-
}
5145

46+
# Normalize the parameters
47+
if (length(label) == 0 || identical(label, "")) label <- " "
48+
if (length(input_path) == 0 || identical(input_path, "")) input_path <- list()
49+
50+
# Start of assertions
5251
checkmate::assert_string(label)
52+
5353
checkmate::assert(
5454
checkmate::check_list(input_path, types = "character", min.len = 0),
5555
checkmate::check_character(input_path, min.len = 1)
5656
)
57-
5857
if (length(input_path) > 0) {
5958
valid_url <- function(url_input, timeout = 2) {
6059
con <- try(url(url_input), silent = TRUE)
6160
check <- suppressWarnings(try(open.connection(con, open = "rt", timeout = timeout), silent = TRUE)[1])
6261
try(close.connection(con), silent = TRUE)
63-
ifelse(is.null(check), TRUE, FALSE)
62+
is.null(check)
6463
}
6564
idx <- vapply(input_path, function(x) file.exists(x) || valid_url(x), logical(1))
6665

@@ -78,8 +77,9 @@ tm_file_viewer <- function(label = "File Viewer Module",
7877
"No file or url paths were provided."
7978
)
8079
}
80+
# End of assertions
8181

82-
82+
# Make UI args
8383
args <- as.list(environment())
8484

8585
module(

R/tm_front_page.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,18 @@ tm_front_page <- function(label = "Front page",
6767
additional_tags = tagList(),
6868
footnotes = character(0),
6969
show_metadata = FALSE) {
70+
logger::log_info("Initializing tm_front_page")
71+
72+
# Start of assertions
7073
checkmate::assert_string(label)
7174
checkmate::assert_character(header_text, min.len = 0, any.missing = FALSE)
7275
checkmate::assert_list(tables, types = "data.frame", names = "named", any.missing = FALSE)
7376
checkmate::assert_multi_class(additional_tags, classes = c("shiny.tag.list", "html"))
7477
checkmate::assert_character(footnotes, min.len = 0, any.missing = FALSE)
7578
checkmate::assert_flag(show_metadata)
79+
# End of assertions
7680

77-
logger::log_info("Initializing tm_front_page")
81+
# Make UI args
7882
args <- as.list(environment())
7983

8084
module(

R/tm_g_association.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,30 +133,43 @@ tm_g_association <- function(label = "Association",
133133
post_output = NULL,
134134
ggplot2_args = teal.widgets::ggplot2_args()) {
135135
logger::log_info("Initializing tm_g_association")
136+
137+
# Normalize the parameters
136138
if (inherits(ref, "data_extract_spec")) ref <- list(ref)
137139
if (inherits(vars, "data_extract_spec")) vars <- list(vars)
138140
if (inherits(ggplot2_args, "ggplot2_args")) ggplot2_args <- list(default = ggplot2_args)
139141

142+
# Start of assertions
140143
checkmate::assert_string(label)
144+
141145
checkmate::assert_list(ref, types = "data_extract_spec")
142146
if (!all(vapply(ref, function(x) !x$select$multiple, logical(1)))) {
143147
stop("'ref' should not allow multiple selection")
144148
}
149+
145150
checkmate::assert_list(vars, types = "data_extract_spec")
146151
checkmate::assert_flag(show_association)
152+
147153
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE)
148154
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height")
149155
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE)
150156
checkmate::assert_numeric(
151157
plot_width[1],
152158
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width"
153159
)
160+
154161
distribution_theme <- match.arg(distribution_theme)
155162
association_theme <- match.arg(association_theme)
163+
164+
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
165+
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
166+
156167
plot_choices <- c("Bivariate1", "Bivariate2")
157168
checkmate::assert_list(ggplot2_args, types = "ggplot2_args")
158169
checkmate::assert_subset(names(ggplot2_args), c("default", plot_choices))
170+
# End of assertions
159171

172+
# Make UI args
160173
args <- as.list(environment())
161174

162175
data_extract_list <- list(

R/tm_g_bivariate.R

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
194194
pre_output = NULL,
195195
post_output = NULL) {
196196
logger::log_info("Initializing tm_g_bivariate")
197+
198+
# Normalize the parameters
197199
if (inherits(x, "data_extract_spec")) x <- list(x)
198200
if (inherits(y, "data_extract_spec")) y <- list(y)
199201
if (inherits(row_facet, "data_extract_spec")) row_facet <- list(row_facet)
@@ -202,52 +204,36 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
202204
if (inherits(fill, "data_extract_spec")) fill <- list(fill)
203205
if (inherits(size, "data_extract_spec")) size <- list(size)
204206

207+
# Start of assertions
208+
checkmate::assert_string(label)
209+
205210
checkmate::assert_list(x, types = "data_extract_spec")
206-
if (!all(vapply(x, function(x) !x$select$multiple, logical(1)))) {
207-
stop("'x' should not allow multiple selection")
208-
}
211+
assert_single_selection(x)
212+
209213
checkmate::assert_list(y, types = "data_extract_spec")
210-
if (!all(vapply(y, function(x) !x$select$multiple, logical(1)))) {
211-
stop("'y' should not allow multiple selection")
212-
}
214+
assert_single_selection(y)
215+
213216
checkmate::assert_list(row_facet, types = "data_extract_spec", null.ok = TRUE)
214-
if (!all(vapply(row_facet, function(x) !x$select$multiple, logical(1)))) {
215-
stop("'row_facet' should not allow multiple selection")
216-
}
217+
assert_single_selection(row_facet)
218+
217219
checkmate::assert_list(col_facet, types = "data_extract_spec", null.ok = TRUE)
218-
if (!all(vapply(col_facet, function(x) !x$select$multiple, logical(1)))) {
219-
stop("'col_facet' should not allow multiple selection")
220-
}
220+
assert_single_selection(col_facet)
221+
222+
checkmate::assert_flag(facet)
223+
221224
checkmate::assert_list(color, types = "data_extract_spec", null.ok = TRUE)
222-
if (!all(vapply(color, function(x) !x$select$multiple, logical(1)))) {
223-
stop("'color' should not allow multiple selection")
224-
}
225+
assert_single_selection(color)
226+
225227
checkmate::assert_list(fill, types = "data_extract_spec", null.ok = TRUE)
226-
if (!all(vapply(fill, function(x) !x$select$multiple, logical(1)))) {
227-
stop("'fill' should not allow multiple selection")
228-
}
228+
assert_single_selection(fill)
229+
229230
checkmate::assert_list(size, types = "data_extract_spec", null.ok = TRUE)
230-
if (!all(vapply(size, function(x) !x$select$multiple, logical(1)))) {
231-
stop("'size' should not allow multiple selection")
232-
}
231+
assert_single_selection(size)
233232

234-
ggtheme <- match.arg(ggtheme)
235-
checkmate::assert_string(label)
236233
checkmate::assert_flag(use_density)
237-
checkmate::assert_flag(color_settings)
238-
checkmate::assert_flag(free_x_scales)
239-
checkmate::assert_flag(free_y_scales)
240-
checkmate::assert_flag(rotate_xaxis_labels)
241-
checkmate::assert_flag(swap_axes)
242-
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE)
243-
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height")
244-
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE)
245-
checkmate::assert_numeric(
246-
plot_width[1],
247-
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width"
248-
)
249-
checkmate::assert_class(ggplot2_args, "ggplot2_args")
250234

235+
# Determines color, fill & size if they are not explicitly set
236+
checkmate::assert_flag(color_settings)
251237
if (color_settings) {
252238
if (is.null(color)) {
253239
color <- x
@@ -267,6 +253,28 @@ tm_g_bivariate <- function(label = "Bivariate Plots",
267253
}
268254
}
269255

256+
checkmate::assert_flag(free_x_scales)
257+
checkmate::assert_flag(free_y_scales)
258+
259+
checkmate::assert_numeric(plot_height, len = 3, any.missing = FALSE, finite = TRUE)
260+
checkmate::assert_numeric(plot_height[1], lower = plot_height[2], upper = plot_height[3], .var.name = "plot_height")
261+
checkmate::assert_numeric(plot_width, len = 3, any.missing = FALSE, null.ok = TRUE, finite = TRUE)
262+
checkmate::assert_numeric(
263+
plot_width[1],
264+
lower = plot_width[2], upper = plot_width[3], null.ok = TRUE, .var.name = "plot_width"
265+
)
266+
267+
checkmate::assert_flag(rotate_xaxis_labels)
268+
checkmate::assert_flag(swap_axes)
269+
270+
ggtheme <- match.arg(ggtheme)
271+
checkmate::assert_class(ggplot2_args, "ggplot2_args")
272+
273+
checkmate::assert_multi_class(pre_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
274+
checkmate::assert_multi_class(post_output, c("shiny.tag", "shiny.tag.list", "html"), null.ok = TRUE)
275+
# End of assertions
276+
277+
# Make UI args
270278
args <- as.list(environment())
271279

272280
data_extract_list <- list(

0 commit comments

Comments
 (0)