Skip to content

Commit f2519ce

Browse files
committed
push
1 parent daaca41 commit f2519ce

File tree

7 files changed

+108
-31
lines changed

7 files changed

+108
-31
lines changed

R/fda-table_32.R

+35-9
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ make_table_32 <- function(advs,
8989
#'
9090
#' @export
9191
make_table_32_gtsum <- function(advs,
92-
adsl,
92+
alt_counts_df = NULL,
9393
id_var = "USUBJID",
9494
arm_var = "ARM",
9595
saffl_var = "SAFFL",
9696
lbl_overall = NULL) {
97+
9798
checkmate::assert_subset(c(
9899
saffl_var, "AVISITN", "PARAMCD", "AVAL", "AVALU", arm_var, id_var
99100
), names(advs))
@@ -109,15 +110,39 @@ make_table_32_gtsum <- function(advs,
109110
group_by(.data[[id_var]], PARAMCD) %>%
110111
mutate(MAX_DIABP = max(AVAL)) %>%
111112
ungroup() %>%
113+
distinct(.data[[id_var]], .keep_all = TRUE) %>%
114+
select(all_of(id_var), MAX_DIABP, AVALU, all_of(arm_var))
115+
116+
if (!is.null(alt_counts_df)) {
117+
adsl <- alt_counts_df %>%
118+
filter(.data[[saffl_var]] == "Y") %>%
119+
select(all_of(id_var), all_of(arm_var), all_of(saffl_var))
120+
121+
advs <- advs %>% select(!all_of(arm_var))
122+
advs <-
123+
adsl %>%
124+
left_join(advs, by = id_var)
125+
}
126+
else {
127+
advs <- advs
128+
}
129+
130+
advs <- advs %>%
112131
mutate(
113-
L60 = with_label(MAX_DIABP < 60, "<60"),
114-
G60 = with_label(MAX_DIABP > 60, ">60"),
115-
G90 = with_label(MAX_DIABP > 90, ">90"),
116-
G110 = with_label(MAX_DIABP > 110, ">110"),
117-
GE120 = with_label(MAX_DIABP >= 120, ">=120")
132+
L60f = case_when(MAX_DIABP < 60 ~ "true", TRUE ~ "false"),
133+
G60f = case_when(MAX_DIABP > 60 ~ "true", TRUE ~ "false"),
134+
G90f = case_when(MAX_DIABP > 90 ~ "true", TRUE ~ "false"),
135+
G110f = case_when(MAX_DIABP > 110 ~ "true", TRUE ~ "false"),
136+
GE120f = case_when(MAX_DIABP >= 120 ~ "true", TRUE ~ "false")
118137
) %>%
119-
distinct(.data[[id_var]], .keep_all = TRUE) %>%
120-
select(L60, G60, G90, G110, GE120, arm_var, AVALU)
138+
mutate(
139+
L60 = with_label(L60f == "true", "<60"),
140+
G60 = with_label(G60f == "true", ">60"),
141+
G90 = with_label(G90f == "true", ">90"),
142+
G110 = with_label(G110f == "true", ">110"),
143+
GE120 = with_label(GE120f == "true", ">=120")
144+
) %>%
145+
select(L60, G60, G90, G110, GE120, AVALU, arm_var)
121146

122147
avalu <- unique(advs$AVALU)[1]
123148
advs <- advs %>% select(-AVALU)
@@ -126,7 +151,8 @@ make_table_32_gtsum <- function(advs,
126151
tbl_summary(
127152
by = arm_var,
128153
statistic = list(all_categorical() ~ "{n} ({p}%)"),
129-
digits = everything() ~ c(0, 1)
154+
digits = everything() ~ c(0, 1),
155+
missing = "no"
130156
) %>%
131157
modify_header(label ~ paste0("**Diastolic Blood Pressure (", avalu, ")**")) %>%
132158
modify_header(all_stat_cols() ~ "**{level}** \nN = {n}") %>%

R/fda-table_33.R

+27-7
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ make_table_33 <- function(advs,
8282
#'
8383
#' @export
8484
make_table_33_gtsum <- function(advs,
85-
adsl,
85+
alt_counts_df = NULL,
8686
id_var = "USUBJID",
8787
arm_var = "ARM",
8888
saffl_var = "SAFFL",
@@ -105,8 +105,10 @@ make_table_33_gtsum <- function(advs,
105105
) %>%
106106
ungroup() %>%
107107
mutate(
108-
SBP90 = formatters::with_label(PARAMCD == "SYSBP" & MAX_SYSBP < 90, "SBP <90"),
109-
DBP60 = formatters::with_label(PARAMCD == "DIABP" & MAX_DIABP < 60, "DBP <60")
108+
SBP90c = if_else(MAX_SYSBP < 90, "SBP <90", "Not", missing = "Not"),
109+
DBP60c = if_else(MAX_DIABP < 60, "DBP <60", "Not", missing = "Not"),
110+
SBP90 = formatters::with_label(SBP90c == "SBP <90", "SBP <90"),
111+
DBP60 = formatters::with_label(DBP60c == "DBP <60", "DBP <60")
110112
)
111113

112114
advs_diabp <- advs_all %>%
@@ -120,17 +122,35 @@ make_table_33_gtsum <- function(advs,
120122
select(all_of(id_var), SBP90)
121123

122124
advs_combined <-
123-
full_join(advs_diabp, advs_sysbp, by = "USUBJID") %>%
124-
select(SBP90, DBP60, ARM, AVALU)
125+
full_join(advs_diabp, advs_sysbp, by = id_var) %>%
126+
select(SBP90, DBP60, ARM, AVALU, all_of(id_var), all_of(arm_var))
127+
128+
if (!is.null(alt_counts_df)) {
129+
adsl <- alt_counts_df %>%
130+
filter(.data[[saffl_var]] == "Y") %>%
131+
select(all_of(id_var), all_of(arm_var), all_of(saffl_var))
132+
133+
advs_combined <-
134+
advs_combined %>%
135+
select(!all_of(arm_var)) %>%
136+
right_join(adsl, by = id_var)
137+
}
138+
else {
139+
advs_combined <- advs_combined
140+
}
141+
125142

126143
avalu <- unique(advs_combined$AVALU)[1]
127-
advs_combined <- advs_combined %>% select(-AVALU)
144+
advs_combined <- advs_combined %>%
145+
# select(-AVALU)
146+
select(SBP90, DBP60, all_of(arm_var))
128147

129148
tbl <- advs_combined %>%
130149
tbl_summary(
131150
by = arm_var,
132151
statistic = list(all_categorical() ~ "{n} ({p}%)"),
133-
digits = everything() ~ c(0, 1)
152+
digits = everything() ~ c(0, 1),
153+
missing = "no"
134154
) %>%
135155
modify_header(label ~ paste0("**Blood Pressure (", avalu, ")**")) %>%
136156
modify_header(all_stat_cols() ~ "**{level}** \nN = {n}") %>%

man/make_table_32.Rd

+1-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/make_table_33.Rd

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

quarto/table-templates/template-table_32.qmd

+13-11
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,19 @@ Required variables:
100100

101101
- **`advs`**: `USUBJID`, `AVISITN`, `PARAMCD`, `AVAL`, `AVALU`, and the variables specified by `arm_var` and `saffl_var`.
102102

103-
+---------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
104-
| Argument | Description | Default |
105-
+:==============+:===================================================================================================================+:=====================+
106-
| `advs` | (`data.frame`) Dataset (typically ADVS) required to build table. | *No default* |
107-
+---------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
108-
| `arm_var` | (`character`) Arm variable used to split table into columns. | `"ARM"` |
109-
+---------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
110-
| `saffl_var` | (`character`) Flag variable used to indicate inclusion in safety population. | `"SAFFL"` |
111-
+---------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
112-
| `lbl_overall` | (`character`) If specified, an overall column will be added to the table with the given value as the column label. | `"Total Population"` |
113-
+---------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
103+
+-----------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
104+
| Argument | Description | Default |
105+
+:================+:===================================================================================================================+:=====================+
106+
| `advs` | (`data.frame`) Dataset (typically ADVS) required to build table. | *No default* |
107+
+-----------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
108+
| `alt_counts_df` | (`character`) Alternative dataset (typically ADSL) used only to calculate column counts. | |
109+
+-----------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
110+
| `arm_var` | (`character`) Arm variable used to split table into columns. | `"ARM"` |
111+
+-----------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
112+
| `saffl_var` | (`character`) Flag variable used to indicate inclusion in safety population. | `"SAFFL"` |
113+
+-----------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
114+
| `lbl_overall` | (`character`) If specified, an overall column will be added to the table with the given value as the column label. | `"Total Population"` |
115+
+-----------------+--------------------------------------------------------------------------------------------------------------------+----------------------+
114116

115117
Source code for this function is available [here](https://github.com/pharmaverse/falcon/blob/main/R/fda-table_32.R).
116118
:::

tests/testthat/test-fda-table_32.R

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
adsl <- adsl_raw
22
advs <- advs_raw
3+
advs_missing <- advs %>%
4+
filter(PARAMCD == "DIABP" & AVISITN >= 1)
5+
set.seed(2)
6+
advs_missing[sample(seq_len(nrow(advs_missing)), 100), "AVAL"] <- NA
7+
advs_missing <- advs_missing %>%
8+
df_explicit_na()
39

410
test_that("Table 32 generation works with default values", {
511
result <- make_table_32(advs, adsl)
@@ -59,3 +65,12 @@ test_that("Table 32 (gtsum) generation works with custom values", {
5965
res <- expect_silent(result)
6066
expect_snapshot(res)
6167
})
68+
69+
test_that("Table 32 (gtsum) generation missing values and ADSL", {
70+
result <- suppressWarnings(
71+
make_table_32_gtsum(advs = advs_missing, alt_counts_df = adsl, lbl_overall = "Total Population") %>% gt::extract_body()
72+
)
73+
74+
res <- expect_silent(result)
75+
expect_snapshot(res)
76+
})

tests/testthat/test-fda-table_33.R

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ adsl <- adsl_raw
22
advs <- advs_raw %>%
33
mutate(AVAL = AVAL - 100)
44

5+
advs_missing <- advs %>%
6+
filter(PARAMCD %in% c("DIABP", "SYSBP") & AVISITN >= 1)
7+
set.seed(2)
8+
advs_missing[sample(seq_len(nrow(advs_missing)), 100), "AVAL"] <- NA
9+
advs_missing <- advs_missing %>%
10+
df_explicit_na()
11+
512
test_that("Table 33 generation works with default values", {
613
result <- make_table_33(advs, adsl)
714

@@ -65,3 +72,12 @@ test_that("Table 33 (gtsum) generation works with custom values", {
6572
res <- expect_silent(result)
6673
expect_snapshot(res)
6774
})
75+
76+
test_that("Table 33 (gtsum) generation missing values and ADSL", {
77+
result <- suppressWarnings(
78+
make_table_33_gtsum(advs = advs_missing, alt_counts_df = adsl, lbl_overall = "Total Population") %>% gt::extract_body()
79+
)
80+
81+
res <- expect_silent(result)
82+
expect_snapshot(res)
83+
})

0 commit comments

Comments
 (0)