Skip to content

Commit

Permalink
Merge branch 'main' into support-subdir-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Melkiades authored Aug 4, 2023
2 parents 19021d6 + 88ab667 commit 709cdd7
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: tern
Title: Create Common TLGs Used in Clinical Trials
Version: 0.8.5.9004
Date: 2023-08-02
Version: 0.8.5.9005
Date: 2023-08-04
Authors@R: c(
person("Joe", "Zhu", , "joe.zhu@roche.com", role = c("aut", "cre")),
person("Daniel", "Sabanés Bové", , "daniel.sabanes_bove@roche.com", role = "aut"),
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# tern 0.8.5.9004
# tern 0.8.5.9005

### Miscellaneous
* Fix swapped descriptions for the `.N_row` and `.N_col` parameters.
* Fix bug in `analyze_vars_in_cols` when categorical data was used.

# tern 0.8.5

Expand Down
8 changes: 8 additions & 0 deletions R/analyze_vars_in_cols.R
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,14 @@ analyze_vars_in_cols <- function(lyt,
# Main statistics
res <- s_summary(u, ...)[[stat]]

if (is.list(res)) {
if (length(res) > 1) {
stop("The analyzed column produced more than one category of results.")
} else {
res <- unlist(res)
}
}

# Label from context
label_from_context <- .spl_context$value[nrow(.spl_context)]

Expand Down
38 changes: 38 additions & 0 deletions tests/testthat/_snaps/analyze_vars_in_cols.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,41 @@
B 9 28.0 0.0 0.0 0.0 0.0
C 9 37.6 0.0 0.0 0.0 0.0

# analyze_vars_in_cols works well with categorical data

Code
build_table(lyt = lyt, df = adpp %>% mutate(counter = factor("n")))
Output
STRATA1 A: Drug X B: Placebo C: Combination
SEX
—————————————————————————————————————————————————
A
F 0 0 81 (100%)
M 0 0 81 (100%)
B
F 0 0 90 (100%)
M 0 0 81 (100%)
C
F 0 0 117 (100%)
M 0 0 72 (100%)

---

Code
basic_table(show_colcounts = TRUE) %>% split_rows_by(var = "STRATA1",
label_pos = "topleft") %>% split_cols_by("ARM") %>% analyze(vars = "SEX",
afun = count_fraction) %>% append_topleft(" SEX") %>% build_table(adpp)
Output
STRATA1 A: Drug X B: Placebo C: Combination
SEX (N=0) (N=0) (N=522)
—————————————————————————————————————————————————
A
F 0 0 81 (16%)
M 0 0 81 (16%)
B
F 0 0 90 (17%)
M 0 0 81 (16%)
C
F 0 0 117 (22%)
M 0 0 72 (14%)

62 changes: 62 additions & 0 deletions tests/testthat/test-analyze_vars_in_cols.R
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,65 @@ testthat::test_that("summarize works with nested analyze", {

testthat::expect_snapshot(tbl_sorted)
})

testthat::test_that("analyze_vars_in_cols works well with categorical data", {
# Regression test after #1013
adpp <- tern_ex_adpp %>% h_pkparam_sort()

lyt <- basic_table() %>%
split_rows_by(var = "STRATA1", label_pos = "topleft") %>%
split_rows_by(
var = "SEX",
label_pos = "topleft",
child_label = "hidden"
) %>%
# split_cols_by("STRATA1") %>%
analyze_vars_in_cols(
vars = "ARM",
.stats = c("n", "count_fraction"),
.labels = c("count_fraction" = "argh")
)
testthat::expect_error(
result <- build_table(lyt = lyt, df = adpp),
"The analyzed column produced more than one category of results."
)

lyt <- basic_table() %>%
split_rows_by(var = "STRATA1", label_pos = "topleft") %>%
split_rows_by(
var = "SEX",
label_pos = "topleft",
child_label = "hidden"
) %>%
split_cols_by("ARM") %>%
analyze_vars_in_cols(
vars = "counter",
.stats = c("count_fraction"),
.labels = c("count_fraction" = " ")
)
testthat::expect_snapshot(build_table(
lyt = lyt,
df = adpp %>% mutate(counter = factor("n"))
))

# Alternative to discuss (xxx)
count_fraction <- function(x, .spl_context, .N_col) { # nolint
ret_list <- as.list(table(x))
if (length(x) == 0) {
aform <- "xx"
} else {
ret_list <- lapply(ret_list, function(i) {
c(i, i / .N_col)
})
aform <- "xx. (xx.%)"
}
in_rows(.list = ret_list, .formats = aform)
}

testthat::expect_snapshot(basic_table(show_colcounts = TRUE) %>%
split_rows_by(var = "STRATA1", label_pos = "topleft") %>%
split_cols_by("ARM") %>%
analyze(vars = "SEX", afun = count_fraction) %>%
append_topleft(" SEX") %>%
build_table(adpp))
})

0 comments on commit 709cdd7

Please sign in to comment.