Skip to content

Commit 1590dfe

Browse files
iaugustygithub-actions[bot]Melkiades
authored
1425 feature request export various internal functions for usage in jj based sister package to nest tern (#1426)
# Pull Request addresses #1425, export several internal functions --------- Signed-off-by: Ilse <[email protected]> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Davide Garolini <[email protected]>
1 parent f69975a commit 1590dfe

20 files changed

+368
-18
lines changed

NAMESPACE

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export(append_varlabels)
4646
export(arrange_grobs)
4747
export(as.rtable)
4848
export(as_factor_keep_attributes)
49+
export(assert_df_with_factors)
50+
export(assert_df_with_variables)
51+
export(assert_proportion_value)
52+
export(check_diff_prop_ci)
53+
export(clogit_with_tryCatch)
4954
export(combine_counts)
5055
export(combine_groups)
5156
export(combine_levels)
@@ -132,6 +137,7 @@ export(g_km)
132137
export(g_lineplot)
133138
export(g_step)
134139
export(g_waterfall)
140+
export(get_covariates)
135141
export(get_formats_from_stats)
136142
export(get_indents_from_stats)
137143
export(get_labels_from_stats)
@@ -226,6 +232,7 @@ export(has_fractions_difference)
226232
export(imputation_rule)
227233
export(keep_content_rows)
228234
export(keep_rows)
235+
export(labels_or_names)
229236
export(labels_use_control)
230237
export(level_order)
231238
export(logistic_regression_cols)
@@ -244,6 +251,7 @@ export(prop_jeffreys)
244251
export(prop_strat_wilson)
245252
export(prop_wald)
246253
export(prop_wilson)
254+
export(range_noinf)
247255
export(reapply_varlabels)
248256
export(ref_group_position)
249257
export(rtable2gg)
@@ -262,6 +270,8 @@ export(s_odds_ratio)
262270
export(s_proportion)
263271
export(s_proportion_diff)
264272
export(s_summary)
273+
export(s_surv_timepoint)
274+
export(s_test_proportion_diff)
265275
export(sas_na)
266276
export(score_occurrences)
267277
export(score_occurrences_cols)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Moved helper functions `h_tab_rsp_one_biomarker()` and `h_tab_surv_one_biomarker()` into `h_biomarkers_subgroups.R`.
3333
* Reorganized the utility documentation related to factors (`utils_factor.R`) into a single file.
3434
* Converted `as_factor_keep_attributes()` to an exported function.
35+
* Converted `assert_df_with_factors()`, `assert_df_with_variables()`, `assert_proportion_value()`, `check_diff_prop_ci()`, `clogit_with_tryCatch()`, `get_covariates()`, `labels_or_names()`, `range_noinf()` , `s_surv_timepoint()`, `s_test_proportion_diff()` to exported functions.
3536
* Removed internal function `ungroup_stats()` and replaced its usage with the `get_*_from_stats()` functions.
3637
* Removed `s_count_nonmissing()` as it is a non-repeated small and internal function.
3738

R/prop_diff.R

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,13 +326,27 @@ estimate_proportion_diff <- function(lyt,
326326

327327
#' Check proportion difference arguments
328328
#'
329+
#' @description `r lifecycle::badge("stable")`
330+
#'
329331
#' Verifies that and/or convert arguments into valid values to be used in the
330332
#' estimation of difference in responder proportions.
331333
#'
332334
#' @inheritParams prop_diff
333335
#' @inheritParams prop_diff_wald
334336
#'
335-
#' @keywords internal
337+
#' @examples
338+
#' # example code
339+
#' ## "Mid" case: 4/4 respond in group A, 1/2 respond in group B.
340+
#' nex <- 100 # Number of example rows
341+
#' dta <- data.frame(
342+
#' "rsp" = sample(c(TRUE, FALSE), nex, TRUE),
343+
#' "grp" = sample(c("A", "B"), nex, TRUE),
344+
#' "f1" = sample(c("a1", "a2"), nex, TRUE),
345+
#' "f2" = sample(c("x", "y", "z"), nex, TRUE),
346+
#' stringsAsFactors = TRUE
347+
#' )
348+
#' check_diff_prop_ci(rsp = dta[["rsp"]], grp = dta[["grp"]], conf_level = 0.95)
349+
#' @export
336350
check_diff_prop_ci <- function(rsp,
337351
grp,
338352
strata = NULL,

R/prop_diff_test.R

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,27 @@ NULL
2626
#' * `s_test_proportion_diff()` returns a named `list` with a single item `pval` with an attribute `label`
2727
#' describing the method used. The p-value tests the null hypothesis that proportions in two groups are the same.
2828
#'
29-
#' @keywords internal
29+
#' @examples
30+
#'
31+
#' ## "Mid" case: 4/4 respond in group A, 1/2 respond in group B.
32+
#' nex <- 100 # Number of example rows
33+
#' dta <- data.frame(
34+
#' "rsp" = sample(c(TRUE, FALSE), nex, TRUE),
35+
#' "grp" = sample(c("A", "B"), nex, TRUE),
36+
#' "f1" = sample(c("a1", "a2"), nex, TRUE),
37+
#' "f2" = sample(c("x", "y", "z"), nex, TRUE),
38+
#' stringsAsFactors = TRUE
39+
#' )
40+
#' s_test_proportion_diff(
41+
#' df = subset(dta, grp == "A"),
42+
#' .var = "rsp",
43+
#' .ref_group = subset(dta, grp == "B"),
44+
#' .in_ref_col = FALSE,
45+
#' variables = NULL,
46+
#' method = "chisq"
47+
#' )
48+
#'
49+
#' @export
3050
s_test_proportion_diff <- function(df,
3151
.var,
3252
.ref_group,

R/survival_timepoint.R

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,25 @@ NULL
4242
#' * `rate_ci`: Confidence interval for event free rate.
4343
#' * `event_free_rate_3d`: Event-free rate (%) with Confidence interval.
4444
#'
45-
#' @keywords internal
45+
#' @examples
46+
#' library(dplyr)
47+
#'
48+
#' adtte_f <- tern_ex_adtte %>%
49+
#' filter(PARAMCD == "OS") %>%
50+
#' mutate(
51+
#' AVAL = day2month(AVAL),
52+
#' is_event = CNSR == 0
53+
#' )
54+
#'
55+
#' s_surv_timepoint(
56+
#' df = subset(adtte_f, ARMCD == "ARM A"),
57+
#' .var = "AVAL",
58+
#' is_event = "is_event",
59+
#' time_point = c(10),
60+
#' control = control_surv_timepoint()
61+
#' )
62+
#'
63+
#' @export
4664
s_surv_timepoint <- function(df,
4765
.var,
4866
time_point,

R/utils.R

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#' Re-implemented `range()` default S3 method for numerical objects
22
#'
3+
#' @description `r lifecycle::badge("stable")`
4+
#'
35
#' This function returns `c(NA, NA)` instead of `c(-Inf, Inf)` for zero-length data
46
#' without any warnings.
57
#'
@@ -9,7 +11,13 @@
911
#'
1012
#' @return A 2-element vector of class `numeric`.
1113
#'
12-
#' @keywords internal
14+
#' @examples
15+
#' x <- rnorm(20, 1)
16+
#' range_noinf(x, na.rm = TRUE)
17+
#' range_noinf(rep(NA, 20), na.rm = TRUE)
18+
#' range(rep(NA, 20), na.rm = TRUE)
19+
#'
20+
#' @export
1321
range_noinf <- function(x, na.rm = FALSE, finite = FALSE) { # nolint
1422

1523
checkmate::assert_numeric(x)
@@ -60,12 +68,17 @@ f_pval <- function(test_mean) {
6068

6169
#' Utility function to return a named list of covariate names
6270
#'
71+
#' @description `r lifecycle::badge("stable")`
72+
#'
6373
#' @param covariates (`character`)\cr a vector that can contain single variable names (such as
6474
#' `"X1"`), and/or interaction terms indicated by `"X1 * X2"`.
6575
#'
6676
#' @return A named `list` of `character` vector.
6777
#'
68-
#' @keywords internal
78+
#' @examples
79+
#' get_covariates(c("a * b", "c"))
80+
#'
81+
#' @export
6982
get_covariates <- function(covariates) {
7083
checkmate::assert_character(covariates)
7184
cov_vars <- unique(trimws(unlist(strsplit(covariates, "\\*"))))
@@ -434,7 +447,32 @@ reapply_varlabels <- function(x, varlabels, ...) {
434447
x
435448
}
436449

437-
# Wrapper function of survival::clogit so that when model fitting failed, a more useful message would show
450+
#' Wrapper function of survival::clogit
451+
#'
452+
#' When model fitting failed, a more useful message would show.
453+
#'
454+
#' @param formula Model formula.
455+
#' @param data data frame.
456+
#' @param ... further parameters to be added to survival::clogit.
457+
#'
458+
#' @return When model fitting is successful, an object of class "clogit".\cr
459+
#' When model fitting failed, an error message is shown.
460+
#'
461+
#' @examples
462+
#' \dontrun{
463+
#' library(dplyr)
464+
#' adrs_local <- tern_ex_adrs %>%
465+
#' dplyr::filter(ARMCD %in% c("ARM A", "ARM B")) %>%
466+
#' dplyr::mutate(
467+
#' RSP = dplyr::case_when(AVALC %in% c("PR", "CR") ~ 1, TRUE ~ 0),
468+
#' ARMBIN = droplevels(ARMCD)
469+
#' )
470+
#' dta <- adrs_local
471+
#' dta <- dta[sample(nrow(dta)), ]
472+
#' mod <- clogit_with_tryCatch(formula = RSP ~ ARMBIN * AGE + strata(STRATA1), data = dta)
473+
#' }
474+
#'
475+
#' @export
438476
clogit_with_tryCatch <- function(formula, data, ...) { # nolint
439477
tryCatch(
440478
survival::clogit(formula = formula, data = data, ...),

R/utils_checkmate.R

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#' Additional assertions to use with `checkmate`
22
#'
3+
#' @description `r lifecycle::badge("stable")`
4+
#'
35
#' Additional assertion functions which can be used together with the `checkmate` package.
46
#'
57
#' @inheritParams checkmate::assert_factor
@@ -31,7 +33,7 @@ check_list_of_variables <- function(x) {
3133
if (isTRUE(res)) {
3234
res <- checkmate::check_character(unlist(x), min.chars = 1)
3335
}
34-
return(res)
36+
res
3537
}
3638
#' @describeIn assertions Checks whether `x` is a valid list of variable names.
3739
#' `NULL` elements of the list `x` are dropped with `Filter(Negate(is.null), x)`.
@@ -74,7 +76,17 @@ check_df_with_variables <- function(df, variables, na_level = NULL) {
7476
#' Please notice how this produces an error when not all variables are present in the
7577
#' data.frame while the opposite is not required.
7678
#'
77-
#' @keywords internal
79+
#' @examples
80+
#' x <- data.frame(
81+
#' a = 1:10,
82+
#' b = rnorm(10)
83+
#' )
84+
#' assert_df_with_variables(x, variables = list(a = "a", b = "b"))
85+
#'
86+
#' x <- ex_adsl
87+
#' assert_df_with_variables(x, list(a = "ARM", b = "USUBJID"))
88+
#'
89+
#' @export
7890
assert_df_with_variables <- checkmate::makeAssertionFunction(check_df_with_variables)
7991

8092
check_valid_factor <- function(x,
@@ -146,12 +158,20 @@ check_df_with_factors <- function(df,
146158
#' are all factors. Note that the creation of `NA` by direct call of `factor()` will
147159
#' trim `NA` levels out of the vector list itself.
148160
#'
149-
#' @keywords internal
161+
#' @examples
162+
#' x <- ex_adsl
163+
#' assert_df_with_factors(x, list(a = "ARM"))
164+
#'
165+
#' @export
150166
assert_df_with_factors <- checkmate::makeAssertionFunction(check_df_with_factors)
151167

152168
#' @describeIn assertions Check whether `x` is a proportion: number between 0 and 1.
153169
#'
154-
#' @keywords internal
170+
#' @examples
171+
#' assert_proportion_value(0.95)
172+
#' assert_proportion_value(1.0, include_boundaries = TRUE)
173+
#'
174+
#' @export
155175
assert_proportion_value <- function(x, include_boundaries = FALSE) {
156176
checkmate::assert_number(x, lower = 0, upper = 1)
157177
checkmate::assert_flag(include_boundaries)

R/utils_rtables.R

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,23 @@ h_col_indices <- function(table_tree, col_names) {
221221

222222
#' Labels or names of list elements
223223
#'
224-
#' Internal helper function for working with nested statistic function results which typically
224+
#' Helper function for working with nested statistic function results which typically
225225
#' don't have labels but names that we can use.
226226
#'
227227
#' @param x (`list`)\cr a list.
228228
#'
229229
#' @return A `character` vector with the labels or names for the list elements.
230230
#'
231-
#' @keywords internal
231+
#' @examples
232+
#' x <- data.frame(
233+
#' a = 1:10,
234+
#' b = rnorm(10)
235+
#' )
236+
#' labels_or_names(x)
237+
#' var_labels(x) <- c(b = "Label for b", a = NA)
238+
#' labels_or_names(x)
239+
#'
240+
#' @export
232241
labels_or_names <- function(x) {
233242
checkmate::assert_multi_class(x, c("data.frame", "list"))
234243
labs <- sapply(x, obj_label)

_pkgdown.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ reference:
6363
- -analyze_functions
6464
- -estimate_coef
6565
- -summarize_functions
66+
- s_surv_timepoint
67+
- s_test_proportion_diff
6668

6769
- title: Model-Specific Functions
6870
desc: These functions help with fitting or extracting results from specific
@@ -75,6 +77,8 @@ reference:
7577
- starts_with("logistic_")
7678
- starts_with("tidy.")
7779
- univariate
80+
- get_covariates
81+
- clogit_with_tryCatch
7882

7983
- title: Graphs
8084
desc: These function create graphical type output.
@@ -107,6 +111,7 @@ reference:
107111
- -h_tab_one_biomarker
108112
- -h_xticks
109113
- -prop_diff
114+
- check_diff_prop_ci
110115

111116
- title: rtables Helper Functions
112117
desc: These functions help to work with the `rtables` package and may be
@@ -128,12 +133,14 @@ reference:
128133
- to_string_matrix
129134
- groups_list_to_df
130135
- utils_split_funs
136+
- labels_or_names
131137

132138
- title: rtables Formatting Functions
133139
desc: These functions provide customized formatting rules to work with the
134140
`rtables` package.
135141
contents:
136142
- starts_with("format_")
143+
- range_noinf
137144

138145
- title: rtables Scoring Functions
139146
desc: These functions can help with table sorting.

inst/WORDLIST

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ TLG
3535
TLGs
3636
biomarker
3737
biomarkers
38+
clogit
39+
coercible
3840
de
3941
funder
4042
layouting
4143
pharmacokinetic
4244
pre
45+
repo
4346
responder
4447
responders
4548
subtable

man/assertions.Rd

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)