Skip to content

Commit c034839

Browse files
committed
merge main into branch
2 parents 68b9199 + 3d58fee commit c034839

11 files changed

Lines changed: 369 additions & 51 deletions

File tree

R/build.R

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,12 @@
4141
#' VS.VSTESTCD == 'Heart Rate'" contains both
4242
#' VS.VSTESTCD and VS.VSSTRESN as prerequisites, and
4343
#' these columns will be kept through to the ADaM.
44-
#'
44+
#' @param verbose Character string controlling message verbosity. One of:
45+
#' \describe{
46+
#' \item{`"message"`}{Show both warnings and messages (default)}
47+
#' \item{`"warn"`}{Show warnings but suppress messages}
48+
#' \item{`"silent"`}{Suppress all warnings and messages}
49+
#' }
4550
#'
4651
#' @return dataset
4752
#' @export
@@ -55,7 +60,8 @@
5560
#' ds_list <- list(DM = read_xpt(metatools_example("dm.xpt")))
5661
#' build_from_derived(spec, ds_list, predecessor_only = FALSE)
5762
build_from_derived <- function(metacore, ds_list, dataset_name = deprecated(),
58-
predecessor_only = TRUE, keep = FALSE) {
63+
predecessor_only = TRUE, keep = FALSE,
64+
verbose = c("message", "warn", "silent")) {
5965
if (is_present(dataset_name)) {
6066
lifecycle::deprecate_warn(
6167
when = "0.2.0",
@@ -68,6 +74,8 @@ build_from_derived <- function(metacore, ds_list, dataset_name = deprecated(),
6874
}
6975
verify_DatasetMeta(metacore)
7076

77+
verbose <- validate_verbose(verbose)
78+
7179
# Deprecate KEEP = TRUE
7280
keep <- match.arg(as.character(keep), c("TRUE", "FALSE", "ALL", "PREREQUISITE"))
7381
if (keep == "TRUE") {
@@ -114,7 +122,7 @@ build_from_derived <- function(metacore, ds_list, dataset_name = deprecated(),
114122
str_to_lower()
115123
if (!all(ds_names %in% names(ds_list))) {
116124
unknown <- keep(names(ds_list), ~ !. %in% ds_names)
117-
if (length(unknown) > 0) {
125+
if (length(unknown) > 0 && check_warn(verbose)) {
118126
warning(paste0("The following dataset(s) have no predecessors and will be ignored:\n"),
119127
paste0(unknown, collapse = ", "),
120128
call. = FALSE
@@ -124,11 +132,13 @@ build_from_derived <- function(metacore, ds_list, dataset_name = deprecated(),
124132
str_to_upper() %>%
125133
paste0(collapse = ", ")
126134

127-
message(paste0(
128-
"Not all datasets provided. Only variables from ",
129-
ds_using,
130-
" will be gathered."
131-
))
135+
if (check_message(verbose)) {
136+
message(paste0(
137+
"Not all datasets provided. Only variables from ",
138+
ds_using,
139+
" will be gathered."
140+
))
141+
}
132142

133143
# Filter out any variable that come from datasets that aren't present
134144
vars_w_ds <- vars_w_ds %>%
@@ -162,7 +172,7 @@ build_from_derived <- function(metacore, ds_list, dataset_name = deprecated(),
162172
group_by(ds) %>%
163173
group_split() %>%
164174
map(get_variables, ds_list, keep, derirvations) %>%
165-
prepare_join(join_by, names(ds_list)) %>%
175+
prepare_join(join_by, names(ds_list), verbose) %>%
166176
reduce(full_join, by = join_by)
167177
}
168178

@@ -232,10 +242,12 @@ get_variables <- function(x, ds_list, keep, derivations) {
232242
#'
233243
#' @param x List of datasets with all columns added
234244
#' @param keys List of key values to join on
245+
#' @param ds_names Names of datasets
246+
#' @param verbose Verbosity level
235247
#'
236248
#' @return datasets
237249
#' @noRd
238-
prepare_join <- function(x, keys, ds_names) {
250+
prepare_join <- function(x, keys, ds_names, verbose = "message") {
239251
out <- list(x[[1]])
240252

241253
if (length(x) > 1) {
@@ -248,8 +260,8 @@ prepare_join <- function(x, keys, ds_names) {
248260
intersect(colnames(x[[i]]))
249261
drop_cols <- c(drop_cols, conflicting_cols)
250262

251-
if (length(conflicting_cols) > 0) {
252-
cli_inform(c("i" = "Dropping column(s) from {ds_names[[i]]} due to \\
263+
if (length(conflicting_cols) > 0 && check_message(verbose)) {
264+
cli_inform(c("i" = "Dropping column(s) from {ds_names[[i]]} due to \
253265
conflict with {ds_names[[j]]}: {conflicting_cols}."))
254266
}
255267
}
@@ -273,6 +285,12 @@ prepare_join <- function(x, keys, ds_names) {
273285
#' Note: Deprecated in version 0.2.0. The `dataset_name` argument will be removed
274286
#' in a future release. Please use `metacore::select_dataset` to subset the
275287
#' `metacore` object to obtain metadata for a single dataset.
288+
#' @param verbose Character string controlling message verbosity. One of:
289+
#' \describe{
290+
#' \item{`"message"`}{Show both warnings and messages (default)}
291+
#' \item{`"warn"`}{Show warnings but suppress messages}
292+
#' \item{`"silent"`}{Suppress all warnings and messages}
293+
#' }
276294
#'
277295
#' @return Dataset with only specified columns
278296
#' @export
@@ -287,7 +305,8 @@ prepare_join <- function(x, keys, ds_names) {
287305
#' select(USUBJID, SITEID) %>%
288306
#' mutate(foo = "Hello")
289307
#' drop_unspec_vars(data, spec)
290-
drop_unspec_vars <- function(dataset, metacore, dataset_name = deprecated()) {
308+
drop_unspec_vars <- function(dataset, metacore, dataset_name = deprecated(),
309+
verbose = c("message", "warn", "silent")) {
291310
if (is_present(dataset_name)) {
292311
lifecycle::deprecate_warn(
293312
when = "0.2.0",
@@ -299,6 +318,8 @@ drop_unspec_vars <- function(dataset, metacore, dataset_name = deprecated()) {
299318
metacore <- make_lone_dataset(metacore, dataset_name)
300319
}
301320

321+
verbose <- validate_verbose(verbose)
322+
302323
verify_DatasetMeta(metacore)
303324
var_list <- metacore$ds_vars %>%
304325
filter(is.na(supp_flag) | !(supp_flag)) %>%
@@ -308,10 +329,12 @@ drop_unspec_vars <- function(dataset, metacore, dataset_name = deprecated()) {
308329
if (length(to_drop) > 0) {
309330
out <- dataset %>%
310331
select(-all_of(to_drop))
311-
message(paste0(
312-
"The following variable(s) were dropped:\n ",
313-
paste0(to_drop, collapse = "\n ")
314-
))
332+
if (check_message(verbose)) {
333+
message(paste0(
334+
"The following variable(s) were dropped:\n ",
335+
paste0(to_drop, collapse = "\n ")
336+
))
337+
}
315338
} else {
316339
out <- dataset
317340
}

R/labels.R

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ remove_labels <- function(data) {
7575
#' Note: Deprecated in version 0.2.0. The `dataset_name` argument will be removed
7676
#' in a future release. Please use `metacore::select_dataset` to subset the
7777
#' `metacore` object to obtain metadata for a single dataset.
78+
#' @param verbose Character string controlling message verbosity. One of:
79+
#' \describe{
80+
#' \item{`"message"`}{Show both warnings and messages (default)}
81+
#' \item{`"warn"`}{Show warnings but suppress messages}
82+
#' \item{`"silent"`}{Suppress all warnings and messages}
83+
#' }
7884
#'
7985
#' @return Dataframe with labels applied
8086
#' @export
@@ -87,7 +93,8 @@ remove_labels <- function(data) {
8793
#' )
8894
#' dm <- haven::read_xpt(metatools_example("dm.xpt"))
8995
#' set_variable_labels(dm, mc, dataset_name = "DM")
90-
set_variable_labels <- function(data, metacore, dataset_name = deprecated()) {
96+
set_variable_labels <- function(data, metacore, dataset_name = deprecated(),
97+
verbose = c("message", "warn", "silent")) {
9198
if (is_present(dataset_name)) {
9299
lifecycle::deprecate_warn(
93100
when = "0.2.0",
@@ -100,6 +107,8 @@ set_variable_labels <- function(data, metacore, dataset_name = deprecated()) {
100107
}
101108
verify_DatasetMeta(metacore)
102109

110+
verbose <- validate_verbose(verbose)
111+
103112
# Grab out the var names and labels
104113
var_spec <- metacore$var_spec %>%
105114
select(variable, label)
@@ -110,16 +119,15 @@ set_variable_labels <- function(data, metacore, dataset_name = deprecated()) {
110119
dns <- names(data)
111120

112121
# Are there any variables in data not in the metadata
113-
mismatch <- setdiff(dns, ns)
114-
in_meta <- ns[which(ns %in% mismatch)]
115-
in_data <- dns[which(dns %in% mismatch)]
122+
in_meta <- setdiff(ns, dns) # Variables in metadata not in data
123+
in_data <- setdiff(dns, ns) # Variables in data not in metadata
116124

117-
if (length(in_meta) > 0) {
125+
if (length(in_meta) > 0 && check_warn(verbose)) {
118126
wrn <- paste0("Variables in metadata not in data:\n\t", paste0(in_meta, collapse = "\n\t"))
119127
warning(wrn, call. = FALSE)
120128
}
121129

122-
if (length(in_data) > 0) {
130+
if (length(in_data) > 0 && check_warn(verbose)) {
123131
wrn <- paste0("Variables in data not in metadata:\n\t", paste0(in_data, collapse = "\n\t"))
124132
warning(wrn, call. = FALSE)
125133
}

R/supp.R

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,21 @@
66
#' @param idvar IDVAR variable name (provided as a string)
77
#' @param qeval QEVAL value to be populated for this QNAM
88
#' @param qorig QORIG value to be populated for this QNAM
9+
#' @param verbose Character string controlling message verbosity. One of:
10+
#' \describe{
11+
#' \item{`"message"`}{Show both warnings and messages (default)}
12+
#' \item{`"warn"`}{Show warnings but suppress messages}
13+
#' \item{`"silent"`}{Suppress all warnings and messages}
14+
#' }
915
#'
1016
#' @return Observations structured in SUPP format
1117
#' @export
1218
#'
1319
#'
14-
build_qnam <- function(dataset, qnam, qlabel, idvar, qeval, qorig) {
20+
build_qnam <- function(dataset, qnam, qlabel, idvar, qeval, qorig,
21+
verbose = c("message", "warn", "silent")) {
22+
verbose <- validate_verbose(verbose)
23+
1524
# Need QNAM as a variable
1625
qval <- as.symbol(qnam)
1726

@@ -58,7 +67,9 @@ build_qnam <- function(dataset, qnam, qlabel, idvar, qeval, qorig) {
5867
blank_test <- out %>%
5968
pull(QVAL)
6069
if (any(blank_test == "")) {
61-
message(paste0("Empty QVAL rows removed for QNAM = ", unique(out$QNAM)))
70+
if (check_message(verbose)) {
71+
message(paste0("Empty QVAL rows removed for QNAM = ", unique(out$QNAM)))
72+
}
6273
out <- out %>%
6374
filter(QVAL != "")
6475
}

R/utils.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,32 @@ make_lone_dataset <- function(metacore, dataset_name) {
4444
}
4545
metacore
4646
}
47+
48+
#' Check if messages should be displayed
49+
#' @param verbose Verbosity level
50+
#' @noRd
51+
check_message <- function(verbose) {
52+
verbose == "message"
53+
}
54+
55+
#' Check if warnings should be displayed
56+
#' @param verbose Verbosity level
57+
#' @noRd
58+
check_warn <- function(verbose) {
59+
verbose %in% c("message", "warn")
60+
}
61+
62+
#' Validate verbose parameter
63+
#' @param verbose Verbosity level to validate
64+
#' @noRd
65+
validate_verbose <- function(verbose, arg = rlang::caller_arg(verbose), call = rlang::caller_env()) {
66+
choices <- c("message", "warn", "silent")
67+
tryCatch(
68+
match.arg(verbose, choices),
69+
error = function(e) {
70+
cli_abort(c(
71+
"x" = "{.arg {arg}} should be one of: {cli::ansi_collapse(choices, last = ', ')}"
72+
), call = call)
73+
}
74+
)
75+
}

man/build_from_derived.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/build_qnam.Rd

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/drop_unspec_vars.Rd

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/set_variable_labels.Rd

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)