Skip to content

Commit eced258

Browse files
kumamiaollrs-roche
andauthored
fix sorting in tm_t_events (#1394)
Fixes #1393 I took a stab at resolving the sorting issue via assigning `table <- pruned_and_sorted_result`. --------- Signed-off-by: Nina Qi <[email protected]> Co-authored-by: Lluís Revilla <[email protected]>
1 parent f09f4b9 commit eced258

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
### Enhancements
77
* Added `denom` argument in `tm_t_binary_outcome` module.
88

9+
### Bug fixes
10+
* Fixed bug in `tm_t_events` to return sorted table (#1393).
11+
912
# teal.modules.clinical 0.10.0
1013

1114
### Enhancements

R/tm_t_events.R

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ template_events <- function(dataname,
340340
sort_list,
341341
quote({
342342
pruned_and_sorted_result <- pruned_result
343-
pruned_and_sorted_result
344343
})
345344
)
346345
} else {
@@ -357,19 +356,20 @@ template_events <- function(dataname,
357356
sort_list,
358357
quote({
359358
pruned_and_sorted_result <- rtables::trim_rows(pruned_result, criteria = criteria_fun)
360-
pruned_and_sorted_result
361359
})
362360
)
363361
}
364362
} else {
365363
# Sort by decreasing frequency.
366-
sort_list <- add_expr(
367-
sort_list,
368-
substitute(
369-
expr = idx_split_col <- which(sapply(col_paths(table), tail, 1) == sort_freq_col),
370-
env = list(sort_freq_col = sort_freq_col)
364+
if (add_total) {
365+
sort_list <- add_expr(
366+
sort_list,
367+
substitute(
368+
expr = idx_split_col <- which(sapply(col_paths(table), tail, 1) == sort_freq_col),
369+
env = list(sort_freq_col = sort_freq_col)
370+
)
371371
)
372-
)
372+
}
373373

374374
# When the "All Patients" column is present we only use that for scoring.
375375
scorefun_hlt <- if (add_total) {
@@ -378,7 +378,7 @@ template_events <- function(dataname,
378378
quote(cont_n_allcols)
379379
}
380380
scorefun_llt <- if (add_total) {
381-
quote(score_occurrences_cols(col_indices = seq(1, ncol(table))))
381+
quote(score_occurrences_cols(col_indices = idx_split_col))
382382
} else {
383383
quote(score_occurrences)
384384
}
@@ -392,7 +392,6 @@ template_events <- function(dataname,
392392
expr = {
393393
pruned_and_sorted_result <- pruned_result %>%
394394
sort_at_path(path = c(term_var), scorefun = scorefun_llt)
395-
pruned_and_sorted_result
396395
},
397396
env = list(
398397
term_var = term_var,
@@ -435,11 +434,6 @@ template_events <- function(dataname,
435434
)
436435
)
437436
}
438-
439-
sort_list <- add_expr(
440-
sort_list,
441-
quote(table <- pruned_and_sorted_result)
442-
)
443437
}
444438
}
445439
y$sort <- bracket_expr(sort_list)
@@ -841,9 +835,16 @@ srv_t_events_byterm <- function(id,
841835
teal.code::eval_code(merged$anl_q(), as.expression(unlist(my_calls)))
842836
})
843837

838+
table_renamed_q <- reactive({
839+
req(table_q())
840+
within(table_q(), {
841+
table <- pruned_and_sorted_result
842+
})
843+
})
844+
844845
decorated_table_q <- srv_decorate_teal_data(
845846
id = "decorator",
846-
data = table_q,
847+
data = table_renamed_q,
847848
decorators = select_decorators(decorators, "table"),
848849
expr = table
849850
)

tests/testthat/_snaps/tm_t_events.md

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@
4747
"All Patients")
4848
pruned_and_sorted_result <- pruned_result %>% sort_at_path(path = c("AEBODSYS"),
4949
scorefun = cont_n_onecol(idx_split_col)) %>% sort_at_path(path = c("AEBODSYS",
50-
"*", "AEDECOD"), scorefun = score_occurrences_cols(col_indices = seq(1,
51-
ncol(table))))
52-
table <- pruned_and_sorted_result
50+
"*", "AEDECOD"), scorefun = score_occurrences_cols(col_indices = idx_split_col))
5351
}
5452
5553

@@ -107,9 +105,7 @@
107105
"All Patients")
108106
pruned_and_sorted_result <- pruned_result %>% sort_at_path(path = c("AEBODSYS"),
109107
scorefun = cont_n_onecol(idx_split_col)) %>% sort_at_path(path = c("AEBODSYS",
110-
"*", "AEDECOD"), scorefun = score_occurrences_cols(col_indices = seq(1,
111-
ncol(table))))
112-
table <- pruned_and_sorted_result
108+
"*", "AEDECOD"), scorefun = score_occurrences_cols(col_indices = idx_split_col))
113109
}
114110
115111

@@ -147,11 +143,8 @@
147143
148144
$sort
149145
{
150-
idx_split_col <- which(sapply(col_paths(table), tail, 1) ==
151-
"All Patients")
152146
pruned_and_sorted_result <- pruned_result %>% sort_at_path(path = c("CMDECOD"),
153147
scorefun = score_occurrences)
154-
pruned_and_sorted_result
155148
}
156149
157150

@@ -203,7 +196,6 @@
203196
$sort
204197
{
205198
pruned_and_sorted_result <- pruned_result
206-
pruned_and_sorted_result
207199
}
208200
209201

@@ -260,14 +252,12 @@
260252
"All Patients")
261253
pruned_and_sorted_result <- pruned_result %>% sort_at_path(path = c("AEBODSYS"),
262254
scorefun = cont_n_onecol(idx_split_col)) %>% sort_at_path(path = c("AEBODSYS",
263-
"*", "AEDECOD"), scorefun = score_occurrences_cols(col_indices = seq(1,
264-
ncol(table))))
255+
"*", "AEDECOD"), scorefun = score_occurrences_cols(col_indices = idx_split_col))
265256
criteria_fun <- function(tr) {
266257
inherits(tr, "ContentRow")
267258
}
268259
pruned_and_sorted_result <- rtables::trim_rows(pruned_and_sorted_result,
269260
criteria = criteria_fun)
270-
table <- pruned_and_sorted_result
271261
}
272262
273263

@@ -329,14 +319,12 @@
329319
"All Patients")
330320
pruned_and_sorted_result <- pruned_result %>% sort_at_path(path = c("AEBODSYS"),
331321
scorefun = cont_n_onecol(idx_split_col)) %>% sort_at_path(path = c("AEBODSYS",
332-
"*", "AEDECOD"), scorefun = score_occurrences_cols(col_indices = seq(1,
333-
ncol(table))))
322+
"*", "AEDECOD"), scorefun = score_occurrences_cols(col_indices = idx_split_col))
334323
criteria_fun <- function(tr) {
335324
inherits(tr, "ContentRow")
336325
}
337326
pruned_and_sorted_result <- rtables::trim_rows(pruned_and_sorted_result,
338327
criteria = criteria_fun)
339-
table <- pruned_and_sorted_result
340328
}
341329
342330

0 commit comments

Comments
 (0)